简体   繁体   English

每次调用popViewControllerAnimated后都会调用viewDidLoad:

[英]viewDidLoad getting called every time after calling popViewControllerAnimated:

I have a vc called ViewControllerOne , when the user taps a UITableViewCell I call a push segue and navigate to ViewControllerTwo . 我有一个名为ViewControllerOne的vc,当用户点击UITableViewCell我调用了一个push segue并导航到ViewControllerTwo In ViewControllerTwo I'm hiding the navigation bar, therefore I've created a custom back button: ViewControllerTwo我隐藏了导航栏,因此创建了一个自定义的后退按钮:

- (IBAction)backBttn:(id)sender {

    [self.navigationController popViewControllerAnimated:YES];

}

that works perfectly, but the viewDidLoad getting called (in ViewControllerTwo ) every time I navigate back to ViewControllerOne and than open ViewControllerTwo again. 可以正常工作,但是每次导航回到ViewControllerOne并再次打开ViewControllerTwo时,都会在ViewControllerTwo调用viewDidLoad Am I right that viewDidLoad getting called because I'm using [self.navigationController popViewControllerAnimated:YES] ? 我正确调用viewDidLoad是因为我正在使用[self.navigationController popViewControllerAnimated:YES]吗? Or it must have another reason? 还是必须有另一个原因?

If I understand you right you are seeing viewDidLoad called on the view controller you are pushing to. 如果我理解正确,那么您会看到正在推送到的视图控制器上调用了viewDidLoad。 ( ViewControllerTwo ). ViewControllerTwo )。 If you push from ViewControllerOne to ViewControllerTwo , ViewControllerTwo 's viewDidLoad is being called. 如果从ViewControllerOne推送到ViewControllerTwo ,则会调用ViewControllerTwo的viewDidLoad。

If you then click the back button to pop ViewControllerTwo , return to ViewControllerOne , then push to ViewControllerTwo again, you see viewDidLoad being called a second time. 如果然后单击后退按钮以弹出ViewControllerTwo ,返回到ViewControllerOne ,然后再次推到ViewControllerTwo ,则会看到第二次调用viewDidLoad

This is expected behavior. 这是预期的行为。 Push segues (and all other segues except unwind segues) create a new instance of the view controller they are presenting. 推式Segue(以及展开式Segue以外的所有其他Segue)将创建它们所呈现的视图控制器的新实例。

Likewise, popping/dismissing deallocates the view controller you are leaving. 同样,弹出/关闭会取消分配您要离开的视图控制器。

When you pop the view controller from the navigation stack, the view that it manages is deallocated (or unloaded). 从导航堆栈中弹出视图控制器时,它所管理的视图将被释放(或卸载)。 Therefore, when you open ViewControllerTwo again, the view must be loaded again. 因此,当再次打开ViewControllerTwo时,必须再次加载视图。 This is why viewDidLoad is called multiple times in your case. 这就是在您的情况下多次调用viewDidLoad原因。

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

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