简体   繁体   English

无法在iOS中获得任何导航项目

[英]Can't get any navigation item in iOS

I have a question regarding the navigation bar. 我对导航栏有疑问。

As far as I understand from iOS: A view controller opened by a segue inherits the navigation bar of the parent view controller. 据我从iOS上了解到:由segue打开的视图控制器继承了父视图控制器的导航栏。 Is this correct so far? 到目前为止,这正确吗?

Is there a view controller within a stack "owns" the navigation bar in a complex segue stack (eg TableViewController that opens a TabBarController that opens ...)? 堆栈中是否有视图控制器“拥有”复杂的segue堆栈中的导航栏(例如,打开的TabViewController打开的TableViewController ...)?

I very often run into the problem that I don't know where to get the actual navigation item in order to set the title or a bar button item. 我经常遇到这样的问题,即我不知道在哪里获得实际的导航项以设置标题或条形按钮项。

In this case, I have the following controllers: 在这种情况下,我具有以下控制器:

  • TabBarController
  • EventPostsViewController -> To display a list of posts, is a tabbed view within the TabBarController EventPostsViewController >要显示帖子列表,是TabBarController中的选项卡式视图
  • CreatePostViewController -> To write a new post CreatePostViewController >撰写新文章

So within the EventPostsViewController I can do this (and it works): 因此,在EventPostsViewController我可以执行此操作(并且可以运行):

class EventPostsViewController: UITableViewController {
    ...
    override func viewWillAppear(animated: Bool) {
        ...
        // This solution works, but only for EventPostsViewController
        self.tabBarController?.navigationItem.title = "text"

But within the CreatePostViewController , which is opened by a segue via EventPostsViewController , neither of this solutions work. 但是在CreatePostViewController内(由EventPostsViewController通过EventPostsViewController打开)中,这两种解决方案都不起作用。

class CreatePostViewController: UIViewController {
    ...
    override func viewWillAppear(animated: Bool) {
        ...
        // Neither of these solutions works
        self.navigationItem.title = "Text"
        self.tabBarController?.navigationItem.title = "Text"
        self.navigationController?.navigationItem.title = "Text"

How do I get the actual navigation bar/navigationItem? 如何获得实际的导航栏/ navigationItem?

Stupid simple mistake I repeat every time :) 我每次都会重复的愚蠢简单错误:)

I forgot to link my custom CreatePostViewController with the view controller using the interface builder. 我忘记了使用界面生成器将自定义CreatePostViewController与视图控制器链接。

在此处输入图片说明

This code now works: 该代码现在可以工作:

class CreatePostViewController: UIViewController {
    ...
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated);
        self.navigationController?.setNavigationBarHidden(false, animated: false)

        // Set title
        self.navigationItem.title = "Write Post"

        // Add Submit button
        var submitButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "submitPost:")
        self.navigationItem.rightBarButtonItem = submitButton
    }
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM