LC 웃
LC 웃

Reputation: 18998

Cannot push view controller to navigation controller using xcode beta 6.3

i am trying to push view controller into navigation controller. The code seems right in xcode 6.1. But when i changed the project into xcode beta6.3 i was asked by xcode to change the typecase operator as to as!. Now i am not able to push view controller into navigation controller

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


        }

    }

Upvotes: 0

Views: 2090

Answers (2)

Sohel L.
Sohel L.

Reputation: 9540

First of all, you need to initialize the root View Controller as navigation controller in AppDelgate.swift, like below:

let navigationController = UINavigationController(rootViewController: MainViewController())

Then, add the following code to push View Controller using the "StoryBoard ID":

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

Upvotes: 0

tskulbru
tskulbru

Reputation: 2346

Technically your viewcontroller can be nil if not found in the storyboard, which is probably why xcode is complaining. A better approach to reference a viewcontroller from the storyboard and pushing it:

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

Here we now only try and push the viewcontroller if the viewcontroller constant moneySummaryVC2 was successfully created, meaning the viewcontroller id was found in the storyboard. Don't forget to handle situations where the viewcontroller wasn't found (logging or something).

Upvotes: 1

Related Questions