无法使用xcode beta 6.3将视图控制器推送到导航控制器

时间:2015-03-20 11:27:34

标签: ios xcode swift uinavigationcontroller

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

//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")


        }

    }

2 个答案:

答案 0 :(得分:1)

从技术上讲,如果在故事板中找不到,你的viewcontroller可能是nil,这可能就是xcode抱怨的原因。从故事板引用视图控制器并推送它的更好方法:

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

这里我们现在只有在成功创建viewcontroller常量moneySummaryVC2时才尝试推送viewcontroller,这意味着在storyboard中找到了viewcontroller id。不要忘记处理未找到viewcontroller的情况(日志记录或其他内容)。

答案 1 :(得分:0)

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

let navigationController = UINavigationController(rootViewController: MainViewController())

然后,添加以下代码以使用“StoryBoard ID”推送View Controller:

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