从SplitViewController中设置数组.viewControllers

时间:2011-11-24 13:37:48

标签: objective-c ipad uisplitviewcontroller master-detail tabbarcontroller

我正在尝试在TabBar中使用splitview。到现在为止,我的第一个TabBarItem中有一个SplitView。当我尝试在SplitViewController中访问不同的DetailView或右视图时出现问题。

我正在尝试在SplitView中的我的Root(或Master)视图控制器的didSelectRowAtIndexPath中执行此操作。

这是代码,我尝试从AppDelegate对象访问我的TabBarController,并更改我的SplitView的viewControllers数组,只更改第二个视图控制器。我总是得到这个崩溃错误,说第二个实例发送无法识别: - [SecondViewController viewControllers]:无法识别的选择器发送到实例0x6852460

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//object AppDelegate
AppDelegate *myDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

//Objecte in Index 0 is my SplitVC
NSArray *barControllers = myDelegate.tabBarController.viewControllers;

if (indexPath.row == 0) 
{
    SplitVC *temporalSplit = [barControllers objectAtIndex:indexPath.row];

    NSArray *mArray = temporalSplit.viewControllers;

    FirstViewController *detail = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];

    UINavigationController *navigationDetail = [[UINavigationController alloc]initWithRootViewController:detail];

    temporalSplit.delegate = detail;

    temporalSplit.viewControllers = [[NSArray alloc] initWithObjects:[mArray objectAtIndex:0], navigationDetail, nil];

    myDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:temporalSplit.viewControllers, [barControllers objectAtIndex:1], nil];

}
else if (indexPath.row == 1) 
{
    SplitVC *temporalSplit = [barControllers objectAtIndex:indexPath.row];

    NSArray *mArray = temporalSplit.viewControllers;

    Detail2VC *detail = [[Detail2VC alloc]initWithNibName:@"Detail2VC" bundle:nil];

    UINavigationController *navigationDetail = [[UINavigationController alloc]initWithRootViewController:detail];

    temporalSplit.delegate = detail;

    temporalSplit.viewControllers = [[NSArray alloc] initWithObjects:[mArray objectAtIndex:0], navigationDetail, nil];


    myDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:[barControllers objectAtIndex:0], temporalSplit, nil];
}
[myDelegate release];
}

我的AppDelegate代码(没有问题):

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

// Override point for customization after application launch.

NSMutableArray *controllersBar = [[NSMutableArray alloc]init];

UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];

for (int i = 0; i<2; i++) 
{
    if(i == 0)
    {
        _firstViewController = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];

        _masterApp = [[MasterVC alloc]initWithNibName:@"MasterVC" bundle:nil];

        _masterApp.firstViewController = _firstViewController;
        _firstViewController.mastervc = _masterApp;

        UINavigationController *navigationMaster = [[UINavigationController alloc]initWithRootViewController:_masterApp];
        UINavigationController *navigationDetail = [[UINavigationController alloc]initWithRootViewController:_firstViewController];

        _splitVC = [[SplitVC alloc] initWithNibName:nil bundle:nil];
        //_splitVC.tabBarItem = controller.tabBarItem;
        _splitVC.viewControllers = [NSArray arrayWithObjects:navigationMaster, navigationDetail, nil];
        _splitVC.delegate = _firstViewController;

        [controllersBar addObject:_splitVC];
    }
    else if (i == 1)
    {
        [controllersBar addObject:viewController2];
    }
}

//self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];

_tabBarController.viewControllers = controllersBar;
_tabBarController.selectedIndex = 0;
_tabBarController.delegate = self;

self.window.rootViewController = self.tabBarController;

[self.window makeKeyAndVisible];

return YES;
}

我做错了什么?欢迎任何帮助或建议。

提前感谢所有人。

1 个答案:

答案 0 :(得分:0)

拆分视图控制器必须是根级控制器:

UISplitviewcontroller not as a rootview controller

如果没有,你显然会发生各种奇怪的事情。