简体   繁体   English

推送 viewController 时未调用 viewDidLoad

[英]viewDidLoad is not being called while pushing a viewController

Here am trying to navigate to another viewController by clicking on a button.这里我试图通过单击按钮导航到另一个 viewController。

It is navigating to next viewController, but viewDidLoad() is not calling here它正在导航到下一个 viewController,但viewDidLoad()没有在这里调用

Here is the code which i wrote to navigate to another viewController on clicking on a button这是我编写的用于在单击按钮时导航到另一个 viewController 的代码

@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)这是我的 viewController(我需要导航)

@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.这里可以进入类,但viewDidLoad()本身没有调用。

How should i achieve this ?我应该如何实现这一目标?

[![Here is my storyboard][1]][1] [![这是我的故事板][1]][1]

这是我与身份检查员的故事板

Your problem is that the class is not settled in IB for that VC;您的问题是该 VC 的课程未在 IB 中解决; make sure class you load and storyboard ID matches the one you want to load.确保您加载的类和故事板 ID 与您要加载的类相匹配。 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:看到你的问题,我想到了两件事:

  1. Check that the nextButtonClicked method is connected to the button (the code there looks OK, so maybe it is just not executed).检查nextButtonClicked方法是否连接到按钮(那里的代码看起来不错,所以可能只是没有执行)。

  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() ).检查您所说的viewDidLoad是否真的在您实例化的LoginViewController中(我希望您正在测试通过在super.viewDidLoad()上设置断点来调用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.当您从故事板实例化 ViewController 的标识符不正确时,经常会发生这种情况。 For eg例如

[[self getStoryboard] instantiateViewControllerWithIdentifier:MyVC];

If MyVC is the identifier of some other ViewController, this might happen.如果MyVC是某个其他 ViewController 的标识符,则可能会发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM