开发者

How do I create a UISplitView manually?

I have an app that is going to navigate to a UISplitView (inside another view altogether) like so:

- (void) switchToMyDayView {
    NSLog(@"Show My Day Screen");

    if (self.myDayController.view.superview == nil) {
        if (self.myDayController == nil) {
            MyDayController *myController = [[MyDayController alloc] initWithNibName:@"MyDay" bundle:nil];
            self.myDayController = myController;
            [myController release];
        }

        [homeScreenController.v开发者_如何学Goiew removeFromSuperview];
        [self.view insertSubview:self.myDayController.view atIndex:0];
    }
}

Which is done on the main navigation screen

Now, the MyDayController has a XIB called MyDay.xib which has these items:

File's Owner: MyDayController

First Responder: UIResponder

Split View Controller

 ---->Navigation Controller

         ---->Navigation Bar

         ----> Table View Controller

                 ----> Navigation Item

 ---->View Controller

So, I need some more components here, I need a UITableViewController and a UISplitViewControllerDelegate correct?

I was going to just implement these protocols in my MyDayController, is this sort of standard?

So, after the code above, I get an error:

-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "MyDay" nib but the view outlet was not set.

so, how can I fix it using the UISplitViewController? I know that the UISplitViewController has a view property, but I cannot use it/connect it up in IB can I?

Thanks a lot

Mark


You shouldn't have to subclass UISplitViewController. What behavior is in your "MyDayController" class? UISplitViewController basically just handles laying out the master and detail views for you, so your responsibility is to implement those controllers.

If the split view is at the top level of your app, you can add it to your app's main window nib. If it isn't, just create it programatically:

- (void) switchToMyDayView {
    NSLog(@"Show My Day Screen");

    if (self.myDayController == nil) {
        YourMasterViewController *masterViewController = [[YourMasterViewController alloc] initWithNibName:@"MasterView" bundle:nil];
        YourDetailViewController *detailViewController = [[YourDetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
        UISplitViewController *myController = [[UISplitViewController alloc] init;
        myController.viewControllers = [NSArray arrayWithObjects:masterViewController, detailViewController, nil];
        [masterViewController release];
        [detailViewController release];

        self.myDayController = myController;
        [myController release];         
    }

    [homeScreenController.view removeFromSuperview];
    [self.view insertSubview:self.myDayController.view atIndex:0];
}

You also don't need the test for self.myDayController.view.superview == nil as it's implicit in self.myDayController == nil


Thank you Christopher Pickslay. This solution works for me but I have to fix something you gave. Please see my code below.

Delegate file

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    LeftViewController *leftViewController = [[LeftViewController alloc] init];// initWithNibName:@"LeftViewController" bundle:nil];
    RightViewController *rightViewController = [[RightViewController alloc] initWithNibName:@"RightViewController" bundle:nil];
    UISplitViewController *myController = [[UISplitViewController alloc] init];
    myController.viewControllers = [NSArray arrayWithObjects:leftViewController, rightViewController, nil];

    self.window.rootViewController = myController;

    [self.window makeKeyAndVisible];
    return YES;
}

Hope this helps.


if you want to create with navigation controller in both master and detail then you can do this

self.rootViewController=[[RootViewController alloc]init];
    self.detailViewController=[[FirstDetailViewController alloc]init];

    UINavigationController *rootNav=[[UINavigationController alloc]initWithRootViewController:rootViewController];
    UINavigationController *detailNav=[[UINavigationController alloc]initWithRootViewController:detailViewController];

    self.splitViewController.viewControllers=[NSArray arrayWithObjects:rootNav,detailNav,nil];
    self.splitViewController.delegate=self.detailViewController;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜