views:

1739

answers:

4

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

+3  A: 

Do you have a UIScrollView in that UIViewController?

mahboudz
Yes, I do!
Denis M
I believe that UIScrollView is taking your firstResponder status away since it wants to get the tap before it is passed on. For all intents and purposes though, I think you're fine since the shake detection mechanism will go through the UIScrollView to your view controller if it needs to.
mahboudz
that's the point - I can't detect shakes anywhere in the code. I started to dig in it and found that VC doesn't become first responder
Denis M
+1  A: 

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

Corey Floyd
I agree with you, but how else would you add shake-detection functionality? I can have the method in one of the UIView subclasses, but how can I guarantee that I will always detect shakes unless the method is in the most parent class?
Denis M
Good point, i updated my answer with some information I found in the dev forums
Corey Floyd
Corey, it worked. The key was to make VC first responder in ViewDidAppear.
Denis M
Now I can only detect shakes on startup. I have UIScrollView with bunch of nested UIViews, and it is still a challenge to make them detect shakes as well.... :(
Denis M
You may have to override the motion began/ended methods in the subviews or at least the scrollview as well and handle the forwarding manually. Passing events passing through UIScrollView has been tricky, it generally likes to swallow (touch) events. Not sure about motion events.
Corey Floyd
+1  A: 

Tip for idiots (me): make sure you set your UIViewController to be first responder after you have assigned some view to your window instance.

fimbaz
A: 

Fimbaz: Thank you! That was the little nugget I was looking for. It's amazing what difference a single line number makes. ;-)

idiotCount++;

;-)

Eric Cartman