【问题标题】:Navigation Controller back button not showing when using a second storyboard使用第二个故事板时导航控制器后退按钮不显示
【发布时间】:2017-01-20 18:21:55
【问题描述】:

我有 2 个故事板。 在 Main.storyboard 我有一个带有按钮的视图控制器。单击按钮时,我加载第二个故事板 Second.storyboard。这是我用来加载 Second.storyboard 的代码

DispatchQueue.main.async {
    let storyBoard : UIStoryboard = UIStoryboard(name: "Second", bundle:nil)

    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "Tools") as UIViewController
    self.present(nextViewController, animated:true, completion:nil)
}

在 Second.storyboard 中,我有一个导航控制器和 2 个视图控制器 A 和 B。

问题是在运行时,我在B中没有看到返回按钮可以回到A。

我尝试过的事情: 我创建了另一个项目。我在 main.storyboard 中嵌入了一个导航控制器和 2 个视图控制器 A 和 B。这工作正常。显示返回按钮。这告诉我加载第二个故事板时我遗漏了一些东西

我已经尝试添加这个

// Do any additional setup after loading the view.
self.title = "Title"

let navigationBar = navigationController!.navigationBar
navigationBar.tintColor = UIColor.blue

let leftButton =  UIBarButtonItem(title: "Left Button", style: UIBarButtonItemStyle.plain, target: self, action: nil)
let rightButton = UIBarButtonItem(title: "Right Button", style: UIBarButtonItemStyle.plain, target: self, action: nil)

navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton

我看到一些加载故事板的示例没有DispatchQueue.main.async {} 这一行。然而;如果我不使用它,我会得到一个错误并且程序崩溃。

如何添加导航控制器?我选择第一个View Controller,Editor,Embed in,Navigation Controller。

我正在尝试在第二个故事板上显示导航控制器,但它没有显示后退按钮。

需要说这是我尝试做的第二个应用程序,非常感谢任何帮助。

【问题讨论】:

    标签: ios xcode storyboard navigationcontroller


    【解决方案1】:

    你在展示“第二”之后看到了什么?您是否看到导航标题/标题区域?只是缺少返回按钮吗?

    • 你是如何从 A 推 B 的?

    • 我怀疑“工具”不是“第二”中的导航控制器。调用 storyBoard.instantiateViewController(withIdentifier: "Tools") 时,您希望引用导航控制器 - 而不是 A。

    【讨论】:

    • Graham Perks,感谢您的代码。很遗憾;它不会加载下一个视图控制器。没有错误。它只是不加载下一个视图。
    • 啊,对不起,我误会了。我改变了答案。
    • 这就是我加载第二个故事板的方式。 DispatchQueue.main.async { let storyBoard : UIStoryboard = UIStoryboard(name: "Second", bundle:nil) let nextViewController = storyBoard.instantiateViewController(withIdentifier: "Tools") as UIViewController self.present(nextViewController, animated:true, completion:无)}。我没有看到任何标题,我没有看到后退按钮。
    【解决方案2】:

    调用 presentViewController 以模态方式呈现视图控制器,在现有导航堆栈之外;它不包含在您的 UINavigationController 或任何其他内容中。如果你想让你的新视图控制器有一个导航栏,你有两个主要选择:

    将新的视图控制器推送到您现有的导航堆栈上,而不是模态显示它:

    让 VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("MyViewController") as!视图控制器 self.navigationController!.pushViewController(VC1, animated: true)

    我在这篇文章中找到了这个答案http://stackoverflow.com/questions/25444213/presenting-viewcontroller-with-navigationviewcontroller-swift

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 2015-05-31
      • 1970-01-01
      相关资源
      最近更新 更多