Why doesn't HypnosisView becomeFirstResponder?


#1

I’m curious, and I think the book should explain this - why all of a sudden HypnosisView doesn’t respond to motion events as before (at the beginning of the chapter)?

Even if you set it to becomeFirstResponder in the loadView method (or anywhere else) it simply refuses to become first responder, even though there is no other first responder (and the canBecomeFirstResponder still returns YES), until you bring in the tab controller. Then it’s just like before.

Obviously the big difference is now there’s a view controller sitting between the UIView and the AppDelegate, but what specifically causes this behavior where the UIView no longer responds to becomeFirstRespnder?

I think this is an issue that should be touched on in the book, since the last chapter dealt so heavily in adding the motion functionality into the UIView.


#2

My solution to this problem: write it into the viewDidAppear method. I guess, if the view is loaded, it does not automatically mean that it is on the display, so it refuses to become first responder.

-(void)viewDidAppear:(BOOL)animated { [super viewDidAppear]; [[self view]becomeFirstResponder]; }


#3

Thanks, TAKeanice, that worked!


#4

Or you could create a custom method wherein you could add first responder code.Like this:-

HypnosisViewController.m

-(void) configureFirstResponder{
    BOOL viewDidBecomeFirstResponder= [self.view becomeFirstResponder];
    NSLog(@"Is First Responder set as HypnosisView? %i",viewDidBecomeFirstResponder);  
}

Then call this method in—
HypnoAppDelegate.m

  self.window.backgroundColor = [UIColor whiteColor];
   [self.window makeKeyAndVisible];
   [viewController configureFirstResponder];
    return YES;

You’re absolutely right. They should have made it clear about this. I tried all that stuff that you mentioned but motion event did not work.
[self.view becomeFirstResponder] would always return false. Don’t know why. Then i created this custom method of mine and added the first responder call into it. Then i called the method in AppDelegate. Still no success. It turned out that [self.window makeKeyAndVisible] was somehow making it not work if you placed your function call before it. Then i placed the custom method function call after that line and viola… it worked. the View finally became first responder.
What is so special about this ‘makeKeyAndVisible’ message. It did not cause a problem in the previous chapter, with the message sending done before that line. :confused:
Please note I faced this confusion before having added tabBarController functionality.