Denis Masyukov
Denis Masyukov

Reputation: 3062

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....

Upvotes: 8

Views: 15403

Answers (3)

Corey Floyd
Corey Floyd

Reputation: 25969

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).

Upvotes: 5

fimbaz
fimbaz

Reputation: 61

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

Upvotes: 6

mahboudz
mahboudz

Reputation: 39376

Do you have a UIScrollView in that UIViewController?

Upvotes: 4

Related Questions