【问题标题】:Cannot push view controller to navigation controller using xcode beta 6.3无法使用 xcode beta 6.3 将视图控制器推送到导航控制器
【发布时间】:2015-03-20 11:27:33
【问题描述】:

我正在尝试将视图控制器推入导航控制器。该代码在 xcode 6.1 中似乎正确。但是当我将项目更改为 xcode beta6.3 时,xcode 要求我将 typecase 运算符更改为 as!。现在我无法将视图控制器推入导航控制器

//delegate method
    func sendIndex(row : Int){

        switch row {

        case 0:

            if(!isCurrentMoneyVc){
            let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let moneySummaryVC: MoneySummaryVC = storyboard.instantiateViewControllerWithIdentifier("moneyVC") as MoneySummaryVC
            //self.navigationController?.pushViewController(moneySummaryVC, animated: true)
            self.navigationController?.setViewControllers([moneySummaryVC], animated: true)
            }else{
                hideMenu()
            }

        case 1:

            if(!isCurrentAboutVc){
            let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let moneySummaryVC1: AccountsVC = storyboard.instantiateViewControllerWithIdentifier("account") as AccountsVC

            self.navigationController?.pushViewController(moneySummaryVC1, animated: true)
            }else{
                hideMenu()
            }

        case 2:

            if(!isCurrentTransactionVc){
            let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let moneySummaryVC2: Transaction = storyboard.instantiateViewControllerWithIdentifier("transact") as Transaction
            self.navigationController?.pushViewController(moneySummaryVC2, animated: true)
            }else{
                hideMenu()
            }

        default:
            println("no index")


        }

    }

【问题讨论】:

    标签: ios xcode swift uinavigationcontroller


    【解决方案1】:

    从技术上讲,如果在情节提要中找不到,您的视图控制器可能为零,这可能是 xcode 抱怨的原因。从情节提要中引用视图控制器并推送它的更好方法:

    if let moneySummaryVC2 = storyboard.instantiateViewControllerWithIdentifier("transact") as? Transaction {
            self.navigationController?.pushViewController(moneySummaryVC2, animated: true)
    }
    

    这里我们现在只在成功创建视图控制器常量 moneySummaryVC2 时尝试推送视图控制器,这意味着在情节提要中找到了视图控制器 ID。不要忘记处理未找到视图控制器的情况(日志记录等)。

    【讨论】:

    • 我解决了这个问题..花了我将近 2 个小时..我应该将根视图控制器初始化为我嵌入的导航控制器。
    【解决方案2】:

    首先,您需要在 AppDelgate.swift 中将根 View Controller 初始化为导航控制器,如下所示:

    let navigationController = UINavigationController(rootViewController: MainViewController())
    

    然后,添加以下代码以使用“StoryBoard ID”推送视图控制器:

    let anotherController: UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("StoryBoardID") as! UIViewController
            self.navigationController?.pushViewController(anotherController, animated: true)
    

    【讨论】:

    • 我是通过情节提要完成的。将根视图控制器初始化为导航控制器。无论如何,谢谢男人!
    猜你喜欢
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    相关资源
    最近更新 更多