SplitViewController UI布局

时间:2011-01-20 17:49:10

标签: objective-c cocoa-touch ipad uitabbarcontroller uisplitviewcontroller

我正在尝试创建一个UI,我有一个SplitView,其中Details区域包含一个TabBarController。 TabBarController将显示在SplitView的RootViewController中选择的项目的3种不同类型的细节。

到目前为止,我已经通过执行以下操作让TabBar显示SPlitView;

1)创建了一个基于SplitView的新应用程序  2)创建了一个新的基于TabBar的应用程序
 3)将TabBar应用程序中的FirstView和SecondView控制器的.xib,.h和.m文件复制到SplitView应用程序中。
 4)将以下内容添加到我的应用程序委托头文件中;

@class RootViewController;
@class DetailViewController;
@class FirstViewController;
@class SecondViewController;

@interface SplitViewTemplateAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;

    UISplitViewController *splitViewController;
    UITabBarController *tabBarController;

    RootViewController *rootViewController;
    DetailViewController *detailViewController;
    FirstViewController *firstViewController;
    SecondViewController *secondViewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet UISplitViewController *splitViewController;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
@property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) IBOutlet FirstViewController *firstViewController;
@property (nonatomic, retain) IBOutlet SecondViewController *secondViewController;

5)在IB中打开MainWindow.xib并将DetailsView上的类更改为UITabController
 6)将以下代码添加到我的应用程序委托模块文件中;

#import "FirstViewController.h"

@synthesize window, splitViewController, rootViewController, detailViewController, tabBarController, firstViewController, secondViewController;

-(void) makeTabBarController {
    NSMutableArray *controllers = [NSMutableArray arrayWithArray:splitViewController.viewControllers];
    int index = 0; 
    for (UIViewController *controller in splitViewController.viewControllers) {
        if (index == 1) {

            //NSLog(@"Index is: %@", index);
            //NSLog(@"Controller name is: %@", controller.title);

            UINavigationController *localNavController;
            tabBarController = [[UITabBarController alloc] init];
            NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:2];

            firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
            localNavController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
            localNavController.tabBarItem.title = @"First Tab";
            [firstViewController release];

            [localViewControllersArray addObject:localNavController];
            [localNavController release]; // Retained by above array

            secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
            localNavController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
            localNavController.tabBarItem.title = @"Second Tab";
            [secondViewController release];

            [localViewControllersArray addObject:localNavController];
            [localNavController release]; // Retained by above array

            tabBarController.viewControllers = localViewControllersArray;
            [localViewControllersArray release]; // Retained thru above setter

            //tabBarController.delegate = splitViewController;
            [controllers replaceObjectAtIndex:index withObject:tabBarController];
        }
        index++;
    }
    splitViewController.viewControllers = controllers;
}

7)在didFinishLaunchingWithOptions方法中添加了以下内容;

[self makeTabBarController];

所以现在我得到一个开箱即用的SplitView,右边有一个标签栏控制器,里面有两个标签。标签用于在视图之间切换。

我现在正在努力解决的几件事情是:

  1. 缺少触发Popover的按钮,是否需要将其添加到每个标签视图?
  2. 如何将RootViewController与TabBarController挂钩,以便显示所选项目的详细信息?

1 个答案:

答案 0 :(得分:0)

不确定你是否还在寻找答案。关于多个细节视图的问题,我遇到了一个非常类似的问题。最后,我在苹果开发者网站上找到并使用了这个解决方案:

SubstitutableDetailViewController

他们的解决方案非常简单易懂,而且非常容易理解。

关于问题的第二部分,您可以让TabBarController委托通知拆分视图左侧的视图当前详细信息视图。然后,它可以使用该信息来提供正确视图的更新。

相关问题