views:

230

answers:

1

I'm having trouble getting UISplitViewController working in a Universal app where I've already coded the iPhone portion. As a troubleshooting method, I decided to start with a new project and just try to do the one action that's causing a problem and it still is.

If I create a Universal app and in the iPad controller create a split view (either in a XIB or in code) then it appears as black (unless I set a background color). If I do it in an iPad-only app, it displays just fine.

I'd appreciate it if anyone could test this on their own and see if they get the same thing, or tell me where I'm going wrong.

  1. In Xcode, create a Universal "Window-based" app.
  2. Go into the iPad controller and paste in the code at the bottom.

What I get is a black screen, not a split view. The same code works in an iPad-only project. What am I doing wrong, or what is configured wrong?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    UISplitViewController *split = [[UISplitViewController alloc] initWithNibName:nil bundle:nil];

    UIViewController *vc1 = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    vc1.view.backgroundColor = [UIColor redColor];

    UIViewController *vc2 = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    vc2.view.backgroundColor = [UIColor blueColor];

    split.viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil];

    [window addSubview:split.view];
    [window makeKeyAndVisible];

    [vc1 release];
    [vc2 release];
    [split release];

    return YES;
}
+1  A: 

First of all, you shouldn't release your split view in didFinishLaunchingWithOptions. Add it to your interface (under UIWindow) and only release it on dealloc. Second, subclass UISplitViewController as follows:

@interface MySplitViewController : UISplitViewController
{
}
@end
@implementation MySplitViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
@end

Third, your didFinishLaunchingWithOptions should look like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    split = [[MySplitViewController alloc] init];

    UIViewController *vc1 = [[UIViewController alloc] init];
    vc1.view.backgroundColor = [UIColor redColor];

    UIViewController *vc2 = [[UIViewController alloc] init];
    vc2.view.backgroundColor = [UIColor blueColor];

    split.viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil];

    [window addSubview:split.view];
    [window makeKeyAndVisible];

    [vc1 release];
    [vc2 release];

    return YES;
}
irlennard
You are right with memory management and the additional iVar. The shouldRotateToInterfaceOrientation: override sounds great too, but it doesn't work for me. Have you tried?
tonklon
If subclassing UISplitViewController doesn't work for you, try subclassing each UIViewController and overriding shouldRotateToInterfaceOrientation: in each one. This is probably the best way to do it anyway...
irlennard
It was the splitview subcontrollers' autorotate and the release issue combined.
codepoet
the important thing was that you cannot initialize `UISplitViewController` using `initWithNibName:bundle:`. It just doesn't work. You have to use `init` instead.