0

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

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

这是代码,我尝试从 AppDelegate 对象访问我的 TabBarController,并更改我的 SplitView 的 viewControllers 数组,只更改第二个视图控制器。我总是收到这个崩溃错误,说第二个实例发送无法识别:-[SecondViewController viewControllers]: unrecognized selector sent to instance 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;
}

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

提前感谢大家。

4

1 回答 1

0

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

UISplitviewcontroller 不作为 rootview 控制器

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

于 2011-11-24T14:00:30.207 回答