is it a good idea to call viewdidload in a method?

徘徊边缘 提交于 2019-12-23 03:23:16

问题


I was just curious weather it is a good idea to call viewdidload a method like in an ibaction or something of that kind.

thanks, TC


回答1:


Check:
UIViewController Class Reference

viewDidLoad

This method is called after the view controller has loaded its associated views into memory. This method is called regardless of whether the views were stored in a nib file or created programmatically in the loadView method. This method is most commonly used to perform additional initialization steps on views that are loaded from nib files.

The viewDidLoad method is automatically triggered.
Generally there is no need to trigger viewDidLoad yourself.
If you need to run specific code both after loading and button-click, do this:

- (void)viewDidLoad {
    [self specificFunction];
}

- (IBAction)theButton:(id)sender {
    [self specificFunction];
}


- (void)specificFunction {
    // This code wil run after the view has been loaded
    // and when the user clicks the button
}



回答2:


The disadvantage of calling ViewDidLoad is , the superclass methods would be called as it has [super viewDidLoad], which is not a good idea . It's better to have a separate methods and call them whenever necessary .



来源:https://stackoverflow.com/questions/6074112/is-it-a-good-idea-to-call-viewdidload-in-a-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!