Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewController - can't become first responder

anyone knows why UIViewController wouldn't become first responder when I run this code:

[self becomeFirstResponder];
NSLog(@"is first resp: %i",[self isFirstResponder]);

In the console, I always see the line: "is first resp: 0"

I HAVE canBecomeFirstResponder method:

- (BOOL)canBecomeFirstResponder {
return YES;
}

I don't even know where to look next....

like image 870
Denis Masyukov Avatar asked Sep 24 '09 05:09

Denis Masyukov


2 Answers

Make sure you set your UIViewController to be first responder after you have assigned some view to your window instance.

like image 59
fimbaz Avatar answered Oct 29 '22 06:10

fimbaz


Update

As I suspected, I assumed wrong about UIViewController/firstResponder usage. This thread in the apple dev forums talks specifically about getting shaking to work.

Original Answer

Call becomeFirstResponder on the UI element that you want to respond. The events will automatically get forwarded to the UIViewController as long as no other objects in the chain implement the touches methods (or at least keep forwarding them up the chain).

Side note: To build on the comments of others, it really doesn't make sense for a UIViewController to be the "first" responder. The first responder should be an object with an on screen representation (a UIView or one of its subclasses).

Although this may be a completely incorrect statement, there may be undocumented behavior in UIViewController that prevents it from becoming the firstResponder because of these issues (Someone smarter than me may be able to verify the validity of this).

like image 20
Corey Floyd Avatar answered Oct 29 '22 06:10

Corey Floyd