views:

516

answers:

1

i am trying to get a subview to become firstResponder. my understanding is that this is done in its viewDidAppear method, like so:

- (void)viewDidAppear {
    [self becomeFirstResponder];
}

while overriding canBecomeFirstResponder to return YES:

- (BOOL)canBecomeFirstResponder {
    return YES;
}

however, when i insert the subview in its parent view's viewDidLoad method:

- (void)viewDidLoad {
    subViewController = [[SubViewController alloc] init];
    [self.view insertSubview: subViewController.view atIndex: 0];
    [subViewController viewDidAppear: NO];
    [super viewDidLoad];

}

(i call viewDidAppear manually, because it does not get triggered automatically), the subview does not become firstResponder.

why does the subview not become firstResponder? and how can i make it firstResponder?

thanks,

mbotta

btw, this is a rewrite of my original question:

i am trying to build an iphone app where a rootviewcontroller object manages two subviews, one of which should react to a user shaking his iphone.

after some digging, i concluded the subview must be made firstResponder in its view controller's viewDidAppear method. moreover, the canBecomeFirstResponder method should be modified to return YES.

so here's what happens: i instantiate the rootviewcontroller in the app delegate. in its viewDidLoad method, i tell it to addSubView firstViewController. all of this works just beautifully.

however, firstViewController does not react to any shaking. i sprinkled some NSLogs around and found that while we DO tell firstViewController in canBecomeFirstResponder to return YES, and while we DO tell it to [self becomeFirstResponder] in viewDidAppear, in actual fact, it is not the firstResponder.

so, my questions are: 1) does a subview actually need to be firstResponder in order to react to shaking? a) if not, how does one make a subview react without being firstResponder? 2) how does one make a subview firstResponder?

what makes this interesting is that if i perform the same sequence (canBecomeFirstResponder, [self firstResponder], motionBegan:) in a different project with only one view controller, it all works flawlessly. clearly, i must be hitting a design flaw of my own making.

thanks,

mbotta

A: 

Not 100% sure, but this could be your problem. If we could see the offending methods it might be easier.

From the Event Handling Best Practices (emphasis added by me):

If you handle events in a subclass of UIView, UIViewController, or (in rare cases) UIResponder,

  • You should implement all of the event-handling methods (even if it is a null implementation).
  • Do not call the superclass implementation of the methods.

If you call the superclass methods the events are probably getting passed along to the nextResponder.

Ben S
thanks! i'll check out how i can do this in my project. in the mean time, i'll re-edit my question to clarify that i am able to get this event if firstViewController is the only view controller.
mbotta