Is it good practice to be setting the delegate of a UITextField in viewDidLoad or in an init method?
I tried setting the delegate as self in an init method, but it wasn’t calling the corresponding delegate methods, when I moved the code into viewDidLoad, it registered as setting self as the delegate?
It seems that I should be able to set it in either method, if someone can help clear this up for me it would be much appreciated.
-(id) init {
self = [super init];
if (self)
textField.delegate = self; //this text field is an IBOutlet
//some other code here as well
return self;
}
OR
-(void)viewDidLoad {
[super viewDidLoad];
textField.delegate = self;
}
If your text field is an
IBOutletthen untilviewDidLoadmethod is called your text field will benil(hence you set delegate tonilobject). WhenviewDidLoadgets called it literally means that view was loaded and all IBOutlets and IBActions were connected and are at your disposal.