Search code examples
iosiphonexcodeswift3

How to go from one view controller in One storyboard to another viewcontroller in another storyboard programmatically?


I have a view controller A in the Storyboard named "X" in it i have a button . On clicking it I am checking the success response for the API and after that I need to navigate to the view controller B in the storyboard "B" .

I am using the code:

let loginstoryboard = UIStoryboard(name: "Login", bundle: nil)
let loginController = loginstoryboard.instantiateViewController(withIdentifier: "login") as? LoginViewController
self.navigationController?.pushViewController(loginController!, animated: true)

So it is not working for me. What is the issue?


Solution

  • Please follow below steps.

    1. Check your both view controllers StoryboardID (If Storyboard ID is already inserted then skip step 2).
    2. Insert View controller StoryboardID

    Please check sample image of StoryboardID

    enter image description here

    Sample Code

       UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:"YourStoryBoardName" bundle:nil];
       UIViewController  *loginViewController = [storyBoard instantiateViewControllerWithIdentifier:"LoginViewController"];
       // If Login View Controller is not a Navigation Controller then you need to create Navigation Controller
       UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
       [self.navigationController pushViewController:viewController animated:true];