UIView doesn't become First Responder when parent UIViewController is set as RootViewController

Go To StackoverFlow.com

2

I'm working on Chapter 7 of BNR's iOS Programming book and I've run into a problem. At the start of the chapter I setup a UIViewController (HypnosisViewController) with an UIView (HypnosisView) that responded to motion events in the previous chapter.

I create the UIViewController in the AppDelegate.m file:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...
    HypnosisViewController *hvc = [[HypnosisViewController alloc] init];
    [[self window] setRootViewController:hvc];
    ...
}

In the HypnosisViewController, I set HypnosisView to become first responder:

- (void)loadView
{
    // Create a view
    CGRect frame = [[UIScreen mainScreen] bounds];
    HypnosisView *view = [[HypnosisView alloc] initWithFrame:frame];
    [self setView:view];

    [view becomeFirstResponder];
}

And in HypnosisView I make sure to return YES to canBecomeFirstResponder. Unfortunately, the HypnosisView did not respond to motion events like before. When I eventually moved on, I made an interesting discovery. If I move HypnosisViewController into a UITabBarController, HypnosisView starts responding to motion events. The code looks something like this:

HypnosisViewController *hvc = [[HypnosisViewController alloc] init];

UITabBarController *tabBarController = [[UITabBarController alloc] init];

NSArray *viewControllers = [NSArray arrayWithObjects:hvc, <insert more objs here>, nil];
[tabBarController setViewControllers:viewControllers];
[[self window] setRootViewController:tabBarController]; 

Why didn't HypnosisView become first responder when HypnosisViewController was set as the RootViewController? Why did it start working once HypnosisViewController was placed inside another controller? What am I missing about RootViewController?

Thanks!

2012-04-05 21:30
by GrandAdmiral


2

Your question is very apt. I'm also studying the same book and am on the same chapter. The thing is that before we used UITabBarController we would either use HypnosisViewController or TimeViewController. And we would then do [self.window setRootViewController:hvc] or [self.window setRootViewController:tvc] in the AppDelegate.m file. In that case setRootViewController method was calling loadView method internally. So if loadView should get called then becomeFirstResponder (which resides inside of it as a method call as per your code) also is supposed to get triggered. So internally canBecomeFirstResponder should get called Now when we use UITabBarController, things tend to break. What happens is instead of loadView getting called via '[[self window] setRootViewController:tabBarController];' line of code, it gets called through '[tabBarController setViewControllers:viewControllers];'. So the bottomline is that rootViewController property (when set to tabBarController) does not call loadView method and hence 'becomeFirstResponder' is not called. You may argue that loadView does get called through '[tabBarController setViewControllers:viewControllers];' but setViewControllers is not used for setting root viewController. When I faced this problem, I made an explicit call to becomeFirstResponder. Here's how:-

@implementation HypnoTimeAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions //method of UIApplicationDelegate protocol  
{
    NSLog(@"lets begin");
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    HypnosisViewController *viewController= [[HypnosisViewController alloc] init];

    TimeViewController *viewController2= [[TimeViewController alloc] init];


    NSLog(@"view controllers are done initializing!");

    UITabBarController *tabBarController= [[UITabBarController alloc] init];
    NSArray *viewControllers= [NSArray arrayWithObjects:viewController,viewController2, nil];

    [tabBarController setViewControllers:viewControllers];//loadView of HypnosisViewController gets called internally since the 'app view' isn't going to load from a XIB file but from 'HypnosisView.m'.loadView method of TimeViewController loads its own view from the XIB file.

    [self.window setRootViewController:tabBarController];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;
}

@implementation HypnosisViewController

 -(void)loadView{

    NSLog(@"HypnosisView loading...");
    HypnosisView *myView= [[HypnosisView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view= myView;
    [self configureFirstResponder];//configuring first responder
}



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

    }
2013-09-03 17:56
by rahulbsb
Ads