Can't push because self.navigationController is nil

自作多情 提交于 2021-02-08 10:12:56

问题


I'm presenting a Navigation Controller as a popover. First time a do it I can push to another View Controller with any issue. But, second time a present another controller the same way I can't perform a push because the self.navigationController of the presented Controller is nil. This is the piece of code I'm using to present the Controller

func instantiateEditController(view : UIView) -> UINavigationController
{
    let popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("Edit Controller") as MCMEditController
    popoverContent.preferredContentSize = CGSizeMake(320, 480)
    let navController = MCMBaseNavigationController(rootViewController: popoverContent)
    navController.modalPresentationStyle = UIModalPresentationStyle.Popover
    navController.navigationBar.tintColor = UIColor.whiteColor()
    let popover = navController.popoverPresentationController
    popover?.sourceView = view
    popover?.sourceRect = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)

    return navController
}

Note: The Navigation Controller is always presented, but just the first time I perform pushes. And this code is for be used in iPad.


回答1:


Because of the nature of Storyboards and not being able to show that here, I'm not sure where your issue is exactly.

However I successfully setup a project that works using segues. I changed your code to the following:

func instantiateEditController(view : UIView) -> UINavigationController
    {
        if let popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("Edit Controller") as? MCMEditController {
            popoverContent.preferredContentSize = CGSizeMake(320, 480)
            let navController = UINavigationController(rootViewController: popoverContent)
            navController.modalPresentationStyle = UIModalPresentationStyle.Popover
            navController.navigationBar.tintColor = UIColor.whiteColor()
            let popover = navController.popoverPresentationController
            popover?.sourceView = view
            popover?.sourceRect = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)

            return navController
        } else {
            return UINavigationController(rootViewController: self) // not recommend to keep this, I'm on Swift 1.2 and this was an easy fix to resolve the errors
        }
    }

To present this popover I set up this IBAction in my ViewController (sender in my case was a UIButton)

@IBAction func presentPopover(sender: AnyObject) {
        if let view = sender as? UIView {
            let controller = instantiateEditController(view)
            let popover = UIPopoverController(contentViewController: controller)
            popover.presentPopoverFromRect(view.frame, inView: self.view, permittedArrowDirections:UIPopoverArrowDirection.Any, animated: true)
        }
    }

The last thing that could be the issue is make sure your segues are using the segue type "show" for all the segues to make sure the transition between them use the navigation controller push:

In Storyboard:

Hope this helps!



来源:https://stackoverflow.com/questions/29656974/cant-push-because-self-navigationcontroller-is-nil

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!