In a UIViewController subclass, I have the following methods:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// do something
myTextField.text = @"Default";
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// do something
[myTextField selectAll:self];
[myTextField becomeFirstResponder];
}
- (void)viewDidLoad {
[super viewDidLoad];
// do something
myTextField.delegate = self;
}
The NIB has been created using Interface Builder. The corresponding view controller object is pushed by the navigation controller through pushViewController.
The inteded behavior is to show a default text entry in a text field, to select the entire text and to set the text field as first responder. [Edit: I've noticed that selecting all and making first responder makes no sense as the selection would dissapear; still, I wonder why the methods behave as described next.]
However, while methods viewDidLoad and viewWillAppear are called, the method viewDidAppear is not called. Can anybody tell me why? Most questions I found on the web and here deal with both viewWillAppear/viewDidAppear are not working; I also understood that in subviews or programmatically created views these methods are not evoked; but this does not apply in case and also I wonder why one of these "lifecycle" methods is evoked and the other not.
Any idea? Thanks!
I had the same problem.
I had copy/pasted
viewDidAppear
to createviewWillAppear
but had forgotten to change thesuper.viewDidAppear()
call. This then seemed to stopviewDidAppear
from being called.It sounds like somewhere in your code you have missed or messed-up a call to the superclass.
This can be because you added a child view controller to your parent VC in
viewDidLoad
orviewWillAppear
. The child's appearance prevents the call toviewDidAppear
.This is a crazy thing to do, and I only know because this was a bug in my code. I meant to add the child VC to this VC, not the parent VC.
I had this issue happen to me:
viewWillAppear
was being called butviewDidAppear
was not!I finally figured out that this was because I had a
tabBarController
where I overloaded it's ownviewDidAppear
and forgot the[super viewDidAppear:animated];
It threw off every
VC
in every tab! adding that line back in fixed it for my otherVC's
.Hope this helps someone!
The call to
viewDidAppear:
should always followviewWillAppear:
unless you are doing something custom, which you say you don't. I don't know why it doesn't work but here are a few ideas:Could it be that you are doing something strange in one of the delegate methods for
UITextFieldDelegate
? It's unlikely that it would affectviewDidAppear:
being called but it could be a culprit.Have you loaded a lot of stuff into memory before pushing the view? I'm not sure what would happen if you got a memory warning between
viewWillAppear:
andviewDidAppear:
.Have you tried to do a Clean? Sometimes that can help.
In cases like these when it should work I usually create a new class and the introduce the functionality one at a the time to see if I can get it work that way. I tried your code in a new Navigation Based project where I added a new
UIViewController
with an outlet to the text field. Then I pasted the code from the question and it did work as expected.