简体   繁体   English

如何手动调用viewDidLoad?

[英]How do I call viewDidLoad Manually?

I'm having a slight problem with iOS. 我在iOS上有一个小问题。 I am passing data back and forth between two view controllers using protocols and manually switching views. 我正在使用协议在两个视图控制器之间来回传递数据并手动切换视图。 My problem is that when I dismiss the top view, viewDidLoad for the bottom view isn't called again. 我的问题是,当我关闭顶视图时,不会再次调用底视图的viewDidLoad。 Since I'm sending information from my second view to my first view, I need viewDidLoad to be called so I can handle the information I'm passing. 由于我是从第二个视图向第一个视图发送信息,因此需要调用viewDidLoad以便处理传递的信息。 If you have any ideas on how to do this, any help is appreciated. 如果您有关于如何执行此操作的任何想法,请提供帮助。 Thank you. 谢谢。

- (void)viewDidLoad {
    [super viewDidLoad];
}

Is called exactly once, when the view controller is first loaded into memory. 第一次将视图控制器加载到内存时,仅调用一次。 This is where you want to instantiate any instance variables and build any views that live for the entire lifecycle of this view controller. 在这里您要实例化任何实例变量并构建在该视图控制器的整个生命周期中都有效的所有视图。

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

Is called when the view is actually visible, and can be called multiple times during the lifecycle of a View Controller (example when a Modal View Controller is dismissed and the view becomes visible again) 在视图实际可见时调用,并且可以在视图控制器的生命周期中多次调用(例如,当关闭模态视图控制器并且视图再次变得可见时)

使用-(void)viewDidAppear:(BOOL)animated而不是viewDidLoad

I am passing data back and forth between two view controllers using protocols and manually switching views. 我正在使用协议在两个视图控制器之间来回传递数据并手动切换视图。 My problem is that when I dismiss the top view, viewDidLoad for the bottom view isn't called again. 我的问题是,当我关闭顶视图时,不会再次调用底视图的viewDidLoad。 Since I'm sending information from my second view to my first view, I need viewDidLoad to be called 由于我正在从第二个视图向第一个视图发送信息,因此需要调用viewDidLoad

No you don't, and the fact that you think you do makes me think something else may be going wrong here. 不,您不会,事实上您认为自己做到了,这让我觉得这里可能有其他问题。

Consider this: if you are truly passing data back from the second view controller from the first, then the first view controller has the data and can update itself now . 试想一下:如果你是真正的从第二视图控制器从第一个将数据传回,然后第一个视图控制器具有的数据, 现在可以自行更新。 It exists, and it has a view. 它存在并且有视图。 So it should update itself on the spot, as you hand it the data. 因此,当您处理数据时,它应该当场更新。 Then, when you dismiss the second view controller, the first view controller's view will appear - already updated. 然后,当您关闭第二个视图控制器时,第一个视图控制器的视图将出现-已经更新。 In effect, dismissing the second view controller merely reveals the first view controller's view, which was there all along, and was updated even though it wasn't showing. 实际上,关闭第二视图控制器仅显示了一直存在的第一视图控制器的视图,即使未显示,该视图也已更新。

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

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