1

I am using Storyboard and want to push a view controller for display in UITabBarController at the click of tab bar item.

For this, I am using an existing ViewController linked to TabBarController created on Storyboard and using below code. However, I get exception, Storyboard doesn't contain a view controller with identifier 'SwitchViewController'

func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {

        let switchViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SwitchViewController")
        self.presentViewController(switchViewController, animated: true, completion: nil)
        return false
    }

UPDATE: I added Storyboard ID for SwitchViewController in Main.storyboard and commented the above code. App Failed with same error when I moved to this tab

I am using iOS 8 and Xcode 7. Kindly help

1
  • I was using correct code, but my first ViewController was missing the rootViewController relationship from NavigationController that caused this issue. Commented Sep 5, 2015 at 9:35

2 Answers 2

3

Check your Storyboard ID,

enter image description here

Try this one,

func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {

    if viewController is SwitchViewController {
        let switchViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SwitchViewController") as! SwitchViewController
        self.presentViewController(switchViewController, animated: true, completion: nil)
        return false
    }

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

5 Comments

I guess you meant to write viewController is SwitchViewController. Anyway, even this code fails. Still same error. Problem is no matter what I do, it always complain, "Unable to find Identifier in the view". I have a UITabBarController and SwitchViewController is attached to tab bar
Could you please list out your steps? Because, for me it fails even if I just add Storyboard ID in the storyboard. What is your version of Xcode?
I tried with a new project and it worked too. But, then what is wrong with my existing project? Before I started the app, I had deleted the default view controller provided by Xcode and instead, used new view controller. Could that be the problem?
That's good to hear. May be, yes. I don't know exactly. I think it is better not to spend your time to trivial and not important things.
It worked. Mistakenly, I had deleted Main.storyboard a week before and after restoring it, I guess the rootViewController info was lost. I did notice some screens breaking, but thought I might lost some connections. 'After re-initiating the rootViewController connection, it finally worked.`
0

Try the following code: let storyboard = UIStoryboard(name: "Main", bundle: nil) let VC = storyboard.instantiateViewControllerWithIdentifier("SwitchViewController")

1 Comment

Tried. Doesn't work. I get the storyboard object, but inside it, it's not able to find the ViewController

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.