1

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Chelsea Shawra
  • 1,593
  • 4
  • 22
  • 43

2 Answers2

2

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];
Vivek
  • 4,916
  • 35
  • 40
0

You can create reference of your story board programmatically below thenceforth instantiate viewcontroller using its storyboard id.

 let loginstoryboard = UIStoryboard(name: "Login", bundle: nil)

In Storyboard You can even reference a view controller in an external storyboard.Refer @bshirley ans for detailed explanation.

luckyShubhra
  • 2,731
  • 1
  • 12
  • 19