显示一个已经在 Navigation Stack 上的 View Controller
Show a View Controller that is already on Navigation Stack
我有一个标签栏控制器(带有底部菜单)和一个顶部菜单。问题是我不想 link 标签栏的黄色和绿色视图(因为用户将使用顶部菜单而不是底部菜单更改视图)。
我遇到一个问题,每次我单击按钮时,都会堆叠一个新的视图实例(所以我最终得到类似 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)
}
}
我可以看到视图在导航堆栈上,因为 for
中的 if
语句是 true
。问题是,我如何检索它而不是推送同一视图的新实例? (因为除了这个存在的巨大内存问题之外,我还丢失了视图上的所有数据)。
我想保持堆栈中的所有内容完好无损。
示例:
V1 -> V2 -> V3 -> V4(当前视图)
如果我从 V4 回到 V1,我仍然希望导航控制器堆栈上有 V4、V3 和 V2。
另一个问题是,如果这个解决方案是苹果可能会拒绝的。
感谢任何帮助。
看起来您不需要使用导航控制器。每当您调用 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)
}
其中每个 'navigation button' 都有唯一的 ID 并调用 didPressTab 函数。
我实际上是从本教程中学到的:https://github.com/codepath/ios_guides/wiki/Creating-a-Custom-Tab-Bar
弹出视图控制器,直到指定的视图控制器位于导航堆栈的顶部。
参考 - https://developer.apple.com/documentation/uikit/uinavigationcontroller
func popToViewController(UIViewController, animated: Bool) -> [UIViewController]?
我有一个标签栏控制器(带有底部菜单)和一个顶部菜单。问题是我不想 link 标签栏的黄色和绿色视图(因为用户将使用顶部菜单而不是底部菜单更改视图)。
我遇到一个问题,每次我单击按钮时,都会堆叠一个新的视图实例(所以我最终得到类似 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)
}
}
我可以看到视图在导航堆栈上,因为 for
中的 if
语句是 true
。问题是,我如何检索它而不是推送同一视图的新实例? (因为除了这个存在的巨大内存问题之外,我还丢失了视图上的所有数据)。
我想保持堆栈中的所有内容完好无损。
示例:
V1 -> V2 -> V3 -> V4(当前视图)
如果我从 V4 回到 V1,我仍然希望导航控制器堆栈上有 V4、V3 和 V2。
另一个问题是,如果这个解决方案是苹果可能会拒绝的。
感谢任何帮助。
看起来您不需要使用导航控制器。每当您调用 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)
}
其中每个 'navigation button' 都有唯一的 ID 并调用 didPressTab 函数。
我实际上是从本教程中学到的:https://github.com/codepath/ios_guides/wiki/Creating-a-Custom-Tab-Bar
弹出视图控制器,直到指定的视图控制器位于导航堆栈的顶部。
参考 - https://developer.apple.com/documentation/uikit/uinavigationcontroller
func popToViewController(UIViewController, animated: Bool) -> [UIViewController]?