0

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


        }

    }

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

i fixed that..took me nearly 2 hours.. i should have initialized root view controller to navigation controller that i have embedded in.
0

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)

1 Comment

i did it through storyboard. Initializing root view controller to navigation controller. Anyway thanks man!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.