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]
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:
Two things come to my mind when looking at your question:
Check that the nextButtonClicked
method is connected to the button (the code there looks OK, so maybe it is just not executed).
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()
).
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)
}
It often happens when the identifier with which you instantiate your ViewController from the storyboard is incorrect. For eg
[[self getStoryboard] instantiateViewControllerWithIdentifier:MyVC];
If MyVC
is the identifier of some other ViewController, this might happen.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.