简体   繁体   English

设置自定义NavigationBar后,Segue无法正常工作

[英]Segue not working after setting up custom navigationBar

I am performing a segue from my loadVC to containterVC in which I set up a navigationController and set it's rootViewController to mainVC 我正在执行从loadVC到containterVC的测试,在其中我设置了navigationController并将其rootViewController设置为mainVC

Code in viewDidLoad in containterVC: 在containterVC的viewDidLoad中的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    mainVC = UIStoryboard.mainVC()  //This is an extension to UIStoryboard
    //Setting up the delegate:
    mainVC.delegate = self




    //rootViewController - ten najbardziej na dole
    myNavigationController = UINavigationController(rootViewController: mainVC)
    //Storing the root view as currentViewController:
    self.currentViewController = mainVC
    myNavigationController.addChildViewController(mainVC)
    self.view.addSubview(myNavigationController.view)
    addChildViewController(myNavigationController)
    myNavigationController.didMoveToParentViewController(self)


    //Trying to set up the navigation bar
    let bar = myNavigationController.navigationBar
    //Navigation bars:
    bar.barTintColor = UIColor.blackColor()
    bar.tintColor = UIColor.whiteColor()
    bar.translucent = false
}

I launch the project. 我启动这个项目。 And? 和? Everything works properly. 一切正常。

Then I try to add these under "bar.translucent = false" line: 然后,我尝试在“ bar.translucent = false”行下添加这些内容:

    print("statement1")
    let menuButton = UIBarButtonItem(title: "Menu", style: UIBarButtonItemStyle.Plain, target: self, action: "")
    print("statement2")
    let item = myNavigationController.navigationItem
    print("statement3")
    item.leftBarButtonItem = menuButton
    print("statement4")
    bar.items = [item]
    print("statement5")

After launching the project print statements up to 4 are displayed and the 5th is not. 启动项目后,最多显示4条打印语句,而第5条则不显示。 So it seems that the segue between loadVC and containterVC is done, but I don't see the mainVC screen that was set up as a rootViewController. 因此,似乎已经完成了loadVC和containterVC之间的任务,但是我看不到设置为rootViewController的mainVC屏幕。

When I delete "bar.items = [item]" line the project is run as it should be, but there is no "Menu" button in the navigation bar. 当我删除“ bar.items = [item]”行时,项目将按原样运行,但是导航栏中没有“菜单”按钮。

What am I doing wrong? 我究竟做错了什么?

You can just use a "container view" from the storyboard and embed a UINavigationController with your mainVC directly within the storyboard. 您可以仅使用情节提要中的“容器视图”,并将UINavigationController与您的mainVC直接嵌入情节提要中。

Anyway i think that myNavigationController.addChildViewController(mainVC) not necessary. 无论如何,我认为myNavigationController.addChildViewController(mainVC)不必要。

Also the bar items should be set to the mainVC's navigationItem, not the navigationController's one. 栏项目也应设置为mainVC的navigationItem,而不是navigationController的。

Also you did not mention what kind of segue are you using. 您也没有提到您正在使用哪种segue。

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

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