1

Here am trying to navigate to another viewController by clicking on a button.

It is navigating to next viewController, but viewDidLoad() is not calling here

Here is the code which i wrote to navigate to another viewController on clicking on a button

@IBAction func nextButtonClicked(_ sender: Any) {
    let OrdersVC = self.storyboard?.instantiateViewController(withIdentifier: “LoginViewController") as! LoginViewController
    self.navigationController?.pushViewController(OrdersVC, animated: true)
}

and here is my viewController (which i need to navigate)

@IBOutlet weak var activeButton: UIButton!
@IBOutlet weak var upcomingButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
}

Here am able to get into the class, but viewDidLoad() itself it is not calling.

How should i achieve this ?

[![Here is my storyboard][1]][1]

Here is my storyboard with identity inspector

Bhanuteja
  • 771
  • 2
  • 8
  • 18
  • So you telling us that you have at least 2 viewControllers in storyboard, first one is root in navigation controller, you calling push 2 vc and did not get viewDidload? Are you seeing transition at least? – tereks Feb 15 '18 at 06:46
  • Can you explain how are you constructing view controllers ? post your storyboard screenshot? – Mohammed Shakeer Feb 15 '18 at 06:55
  • @tereks, yes i am using using more than 2 viewControllers. and first viewController is root view controller. And i embedded that 1st view controller to Navigation Controller. Yes i can see transition. the references which i took in viewController 2 is calling. But inside viewdidload() it is not getting – Bhanuteja Feb 15 '18 at 06:55
  • @SuganyaMarlin. i used viewWillAppear() also. It is also not being called – Bhanuteja Feb 15 '18 at 06:57
  • Actually viewDidLoad() will be called. Show ur storyboard – Catherine Feb 15 '18 at 06:58
  • Did u set ur segue and use self.performSegue(withIdentifier: "segue", sender: nil) ? – Catherine Feb 15 '18 at 06:59
  • @ammateja try this : let storyBoard : UIStoryboard = UIStoryboard(name: “StoryBoardName”, bundle:nil) let nextViewController = storyboard.instantiateViewController(withIdentifier: "yourViewController") self.navigationController?.pushViewController(nextViewController, animated: true) – Dixit Akabari Feb 15 '18 at 07:02
  • i edited my question. storyboard added. @SuganyaMarlin & Mohammed – Bhanuteja Feb 15 '18 at 07:04
  • The only reason you getting this is that you did not connect storyboard vc with actual class itself in identity inspector – tereks Feb 15 '18 at 07:05
  • Actually, if you're doing it right, `viewDidLoad` will be called; It is not even an option, which means that there is probably something wrong of what you did so far. – Ahmad F Feb 15 '18 at 07:10
  • no need to perform segue to work it should work – Shehata Gamal Feb 15 '18 at 07:12
  • @ammateja show your "YourViewController" identity inspector – Dixit Akabari Feb 15 '18 at 07:13
  • i edited my question. i added an image of my storyboard with identity inspector – Bhanuteja Feb 15 '18 at 07:25
  • what do you mean that "`Here am able to get into the class`" what does that mean? – Milan Nosáľ Feb 15 '18 at 08:52
  • @SuganyaMarlin, i want to do this process without any segues and my storyboard is added with identity inspector – Bhanuteja Feb 15 '18 at 09:17
  • @MilanNosáľ, the references which i took in viewController 2 is calling. but inside viewdidLoad() it is not getting – Bhanuteja Feb 15 '18 at 09:19
  • what is viewController 2? what references you took? how do you know it does not execute viewDidLoad? – Milan Nosáľ Feb 15 '18 at 09:20
  • viewController 2 means, loginViewController. i kept break point in viewDidLoad(). it didn't come inside viewDidLoad() – Bhanuteja Feb 15 '18 at 09:21
  • try to make the second lineof the nextButtonClicked to `self.navigationController!.pushViewController(OrdersVC, animated: true)` – Milan Nosáľ Feb 15 '18 at 09:37

4 Answers4

2

Your problem is that the class is not settled in IB for that VC; make sure class you load and storyboard ID matches the one you want to load. For example, to load:

  let OrdersVC = self.storyboard?.instantiateViewController(withIdentifier: “secondID") as! secondViewController
self.navigationController?.pushViewController(OrdersVC, animated: true)

Image:

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
1

Two things come to my mind when looking at your question:

  1. Check that the nextButtonClicked method is connected to the button (the code there looks OK, so maybe it is just not executed).

  2. Check if the viewDidLoad that you are speaking about is really in LoginViewController that you instantiate (and I hope you are testing the fact that viewDidLoad is called by setting a breakpoint on super.viewDidLoad()).

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
  • nextButtonClicked is connected. and after tapping on the button it is coming inside that button action also. – Bhanuteja Feb 15 '18 at 09:24
1

Just write folloiwng line in between your push code

@IBAction func nextButtonClicked(_ sender: Any) {
    let OrdersVC = self.storyboard?.instantiateViewController(withIdentifier: “LoginViewController") as! LoginViewController

_ = OrdersVC.view // write this line

    self.navigationController?.pushViewController(OrdersVC, animated: true)
}
Berlin
  • 2,115
  • 2
  • 16
  • 28
  • wrote that line, now viewDidLoad is calling. But it is not at all navigating to next viewController. it remains in 1st viewController only – Bhanuteja Feb 15 '18 at 11:49
0

It often happens when the identifier with which you instantiate your ViewController from the storyboard is incorrect. For e.g.

[[self getStoryboard] instantiateViewControllerWithIdentifier:MyVC];

If MyVC is the identifier of some other ViewController, this might happen.

iphondroid
  • 498
  • 7
  • 19