问题
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