Search code examples
iphoneobjective-cuitabbarcontrolleruisplitviewcontroller

Another UISplitViewController + UITabBarController issue


I am adding multiple UISplitViewControllers to a single UITabBarController. The tabs appear, and each master/child view controller is loaded and displayed properly when the tab is clicked, BUT somehow user interaction only works on the last tab added.

If I add 1 tab, it works fine, but if I add 2 tabs, the 2nd one works and the 1st tab won't respond to user interaction (nothing happens when clicking user interface elements - they don't even highlight to show the click). If I add 3, the first 2 are break and the 3rd works.

How can I fix this? Here's the code (note: this method is called from a splashpage view controller at the end of a startup routine):

- (void)startIPad 
{
    UINavigationController *localNavigationController;
    UISplitViewController *localSplitViewController;
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    NSMutableArray *localControllersArray = [[NSMutableArray alloc] init];

    //setup the first tab
    Master1ViewController *viewMaster1 = [[Master1ViewController alloc] init];
    Detail1ViewController *viewDetail1 = [[Detail1ViewController alloc] init];
    localNavigationController = [[UINavigationController alloc] initWithRootViewController:viewDetail1];
    localSplitViewController = [[UISplitViewController alloc] init];
    [localSplitViewController setDelegate:viewDetail1];
    localSplitViewController.viewControllers = [NSArray arrayWithObjects:viewMaster1, localNavigationController, nil];
    [localControllersArray addObject:localSplitViewController];
    [viewMaster1 release];
    [viewDetail1 release];
    [localNavigationController release];
    [localSplitViewController release];

    //setup the second tab
    Master2ViewController *viewMaster2 = [[Master2ViewController alloc] init];
    Detail2ViewController *viewDetail2 = [[Detail2ViewController alloc] init];
    localNavigationController = [[UINavigationController alloc] initWithRootViewController:viewDetail2];
    localSplitViewController = [[UISplitViewController alloc] init];
    [localSplitViewController setDelegate:viewDetail2];
    localSplitViewController.viewControllers = [NSArray arrayWithObjects:viewMaster2, localNavigationController, nil];
    [localControllersArray addObject:localSplitViewController];
    [viewMaster2 release];
    [viewDetail2 release];
    [localNavigationController release];
    [localSplitViewController release];

    //setup the third tab
    ...

    //setup the fourth tab
    ...

    //set the UISplitViewControllers onto the tab bar
    tabBarController.viewControllers = localControllersArray;
    [localControllersArray release];

    //switch to the new root view controller
    [appDelegate.window setRootViewController:tabBarController];
    [tabBarController release];
}

Solution

  • I have created a dummy app with your code and its working fine in my app. Just i have initialize the appDelegate like this

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        // Override point for customization after application launch.
        //self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
        //self.window.rootViewController = self.viewController;
        [self startIPad];
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    
    
    - (void)startIPad
    {
        UINavigationController *localNavigationController;
        UISplitViewController *localSplitViewController;
        UITabBarController *tabBarController = [[UITabBarController alloc] init];
        NSMutableArray *localControllersArray = [[NSMutableArray alloc] init];
    
        //setup the first tab
        Master1ViewController *viewMaster1 = [[Master1ViewController alloc] init];
        Detail1ViewController *viewDetail1 = [[Detail1ViewController alloc] init];
        localNavigationController = [[UINavigationController alloc] initWithRootViewController:viewDetail1];
        localSplitViewController = [[UISplitViewController alloc] init];
        [localSplitViewController setDelegate:viewDetail1];
        localSplitViewController.viewControllers = [NSArray arrayWithObjects:viewMaster1, localNavigationController, nil];
        [localControllersArray addObject:localSplitViewController];
        [viewMaster1 release];
        [viewDetail1 release];
        [localNavigationController release];
        [localSplitViewController release];
    
        //setup the second tab
        Master2ViewController *viewMaster2 = [[Master2ViewController alloc] init];
        Detail2ViewController *viewDetail2 = [[Detail2ViewController alloc] init];
        localNavigationController = [[UINavigationController alloc] initWithRootViewController:viewDetail2];
        localSplitViewController = [[UISplitViewController alloc] init];
        [localSplitViewController setDelegate:viewDetail2];
        localSplitViewController.viewControllers = [NSArray arrayWithObjects:viewMaster2, localNavigationController, nil];
        [localControllersArray addObject:localSplitViewController];
        [viewMaster2 release];
        [viewDetail2 release];
        [localNavigationController release];
        [localSplitViewController release];
    
        //setup the third tab
       // ...
    
        //setup the fourth tab
        //...
    
        //set the UISplitViewControllers onto the tab bar
        tabBarController.viewControllers = localControllersArray;
        [localControllersArray release];
    
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
        //switch to the new root view controller
        [appDelegate.window setRootViewController:tabBarController];
        [tabBarController release];
    }