显示已在导航堆栈上的视图控制器

问题描述 投票:2回答:2

我有一个标签栏控制器(带有底部菜单)和顶部菜单。问题是我不想将黄色和绿色视图链接到标签栏(因为用户将使用顶部菜单而不是底部菜单更改视图)。

我遇到一个问题,每次我点击按钮时,视图的新实例都会堆叠(所以我最终得到类似V1 - > V2 - > V3 - > V2 - > V4等等)

我的部分解决方案是做这样的事情:

@IBAction func yellowViewButtonAction(_ sender: AnyObject)
{
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "YelloViewController") as! YelloViewController

    if let viewControllers = navigationController?.viewControllers {
        for viewController in viewControllers {
            // some process
            if viewController is YelloViewController {
                print("View is on stack")
            }
        } 

         let storyboard = UIStoryboard(name: "Main", bundle: nil)
         let controller = storyboard.instantiateViewController(withIdentifier: "YelloViewController") as! YelloViewController
         self.navigationController?.pushViewController(controller, animated: false)
    }
}

我可以看到视图在导航堆栈上,因为if中的for语句是true。问题是,如何检索它而不是推送同一视图的新实例? (因为除了存在的巨大内存问题,我还丢失了我在视图上的任何数据)。

我想保持堆栈中的所有内容完好无损。

例:

V1 - > V2 - > V3 - > V4(当前视图)

如果我从V4回到V1,我仍然想在导航控制器堆栈上安装V4,V3和V2。

另一个问题是,如果这个解决方案是Apple可能会拒绝的。

我感谢任何帮助。

enter image description here

ios swift3 navigationcontroller
2个回答
1
投票

看起来你不使用并需要导航控制器。每当你调用self.navigationController?.pushViewController(controller, animated: false)时,该控制器的新实例正在进行堆栈。

理想情况下,您可以从已导航的视图控制器调用popViewController。在创建标签栏控制器的自定义行为时,至少在我看来,很难完全按照您的计划获得导航逻辑。

在这种情况下,我通常会手动显示和隐藏视图控制器。

@IBAction func didPressTab(sender: UIButton) {
        let previousIndex = selectedIndex
        selectedIndex = sender.tag
        buttons[previousIndex].selected = false
        let previousVC = viewControllers[previousIndex]
        previousVC.willMoveToParentViewController(nil)
        previousVC.view.removeFromSuperview()
        previousVC.removeFromParentViewController()
        sender.selected = true
        let vc = viewControllers[selectedIndex]
        addChildViewController(vc)
        vc.view.frame = contentView.bounds
        contentView.addSubview(vc.view)
        vc.didMoveToParentViewController(self)

    }

其中每个'导航按钮'都有唯一的ID并调用didPressTab函数。

我实际上是从本教程中学到的:qazxsw poi


0
投票

弹出视图控制器,直到指定的视图控制器位于导航堆栈的顶部。

参考 - https://github.com/codepath/ios_guides/wiki/Creating-a-Custom-Tab-Bar

https://developer.apple.com/documentation/uikit/uinavigationcontroller
© www.soinside.com 2019 - 2024. All rights reserved.