无法推送,因为self.navigationController是零

时间:2015-04-15 17:33:53

标签: ios ipad popover

我将导航控制器作为弹出窗口呈现。第一次这样做,我可以推送到另一个View Controller有任何问题。但是,第二次呈现另一个控制器的方式与我无法执行推送相同,因为所呈现的Controller的self.navigationController为零。这是我用来呈现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
}

注意:始终显示导航控制器,但这是我第一次执行推送。此代码适用于iPad。

1 个答案:

答案 0 :(得分:1)

由于故事板的性质而无法在此处显示,我不确定您的问题究竟在哪里。

但是我成功设置了一个使用segues的项目。我将您的代码更改为以下内容:

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
        }
    }

要呈现此popover,我在我的ViewController中设置了此IBAction(在我的情况下,发件人是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)
        }
    }

最后可能出现的问题是确保你的segue使用了segue类型" show"对于所有segue,以确保它们之间的过渡使用导航控制器push:

In Storyboard:

希望这有帮助!