Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UIView and becomeFirstResponder

I have a custom UIView that implements the UIKeyInput protocol and has

- (BOOL) canBecomeFirstResponder{ 
    return YES;
}

defined in the subclass. When calling:

[customView becomeFirstResponder];
NSLog(@"is first? %i",[customView isFirstResponder]);

during a button click, it returns false, even though canBecomeFirstResponder is properly set and all of the UIKeyInput protocol functions are implemented. What other things could be blocking this view from becoming the first responder? It lives inside of a scrollView and another custom view if that helps.

Update:

I checked to see what the current first responder was with:

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView   *firstResponder = [keyWindow performSelector:@selector(firstResponder)];

and surprisingly firstResponder was nil. So nothing seems to be hogging the events.

like image 694
Ralphleon Avatar asked Dec 08 '11 21:12

Ralphleon


1 Answers

Did you override becomeFirstResponder?

Subclasses can override this method to update state or perform some action such as highlighting the selection.

Followup:

Subclasses can override this method to update state or perform some action such as highlighting the selection.

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.

Did you verify you meet all of the above conditions?

like image 102
Steve Avatar answered Oct 04 '22 19:10

Steve