UIViewController: viewWillAppear is called, viewDi

2019-06-14 23:52发布

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!

4条回答
干净又极端
2楼-- · 2019-06-15 00:12

I had the same problem.

I had copy/pasted viewDidAppear to create viewWillAppear but had forgotten to change the super.viewDidAppear() call. This then seemed to stop viewDidAppear from being called.

It sounds like somewhere in your code you have missed or messed-up a call to the superclass.

查看更多
Root(大扎)
3楼-- · 2019-06-15 00:24

This can be because you added a child view controller to your parent VC in viewDidLoad or viewWillAppear. The child's appearance prevents the call to viewDidAppear.

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.

查看更多
唯我独甜
4楼-- · 2019-06-15 00:36

I had this issue happen to me: viewWillAppear was being called but viewDidAppear was not!

I finally figured out that this was because I had a tabBarController where I overloaded it's own viewDidAppear and forgot the [super viewDidAppear:animated];

It threw off every VC in every tab! adding that line back in fixed it for my other VC's.

Hope this helps someone!

查看更多
倾城 Initia
5楼-- · 2019-06-15 00:38

The call to viewDidAppear: should always follow viewWillAppear: 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 affect viewDidAppear: 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: and viewDidAppear:.

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.

查看更多
登录 后发表回答