Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is becomeFirstResponder not working?

I added a modal using AGWindowView. Inside the modal view (built using IB), there is a textfield. The textfield has been connected to an outlet.

This doesn't work:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.placesTextField becomeFirstResponder];
}

The call to becomeFirstResponder doesn't work and the keyboard doesn't show up.

This works:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.placesTextField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0];
}

However, if I manually send a message using performSelector:withObject:afterDelay it works. Why is this method not being determined until runtime?

like image 371
Ravi Avatar asked Nov 26 '13 04:11

Ravi


2 Answers

Seems somehow in iOS7, view/object is not attached in view hierarchy / window yet. So calling method over object fails. If we put some delay and it is working that means at that moment objects are attached to window.

As per Apple,

A responder object only becomes the first responder if the current responder can resign first-responder status (canResignFirstResponder) and the new responder can become first responder.

You may call this method to make a responder object such as a view the first responder. However, you should only call it on that view if it is part of a view hierarchy. If the view’s window property holds a UIWindow object, it has been installed in a view hierarchy; if it returns nil, the view is detached from any hierarchy.

For more details see UIResponder Class Reference.

like image 172
βhargavḯ Avatar answered Nov 09 '22 11:11

βhargavḯ


There is a big difference between your first and second method.

Per the delay parameter of performSelector:withObject:afterDelay:

The minimum time before which the message is sent. Specifying a delay of 0 does not necessarily cause the selector to be performed immediately. The selector is still queued on the thread’s run loop and performed as soon as possible.

The second method will wait until an appropriate time and perform becomeFirstResponder.

like image 27
fujianjin6471 Avatar answered Nov 09 '22 09:11

fujianjin6471