0

I'm trying to use a splitview inside a TabBar. By now I have a SplitView in my first TabBarItem. My problem comes when I try to access to a different DetailView or right view in the SplitViewController I have.

I'm trying to do it inside the didSelectRowAtIndexPath: of my Root (or Master) viewcontroller from the SplitView.

Here's the code, where I try to acces to my TabBarController from an AppDelegate object, and change the viewControllers array of my SplitView only changing the second view controller. I always get this crash error, saying that 2nd instance send is unrecognized: -[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];
}

And my AppDelegate code (that works without problems):

- (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;
}

What am I doing wrong? Any help or suggestion is welcome.

Thanks to all in advance.

Kolky
  • 2,917
  • 1
  • 21
  • 42
agapitocandemor
  • 696
  • 11
  • 25

1 Answers1

0

Split view controller MUST be root level controllers:

UISplitviewcontroller not as a rootview controller

If not, you can apparently get all sorts of strange things happening.

Community
  • 1
  • 1
tarmes
  • 15,366
  • 10
  • 53
  • 87
  • How is SecondViewController defined? – tarmes Nov 24 '11 at 15:21
  • #import interface SecondViewController : UIViewController { } – agapitocandemor Nov 25 '11 at 10:45
  • Is defined as it comes, I started with a tabbar app and put the splitview in the first tabBarItem. SecondViewController is in the second TabBarItem. MasterVC is left view in SplitView, FirstViewController is first right view in splitView, and Im trying to show Detail2VC when I click on the 2nd row of my Master table view, but it always crashes, and is always in the line where I try to modify the .viewControllers array of the SplitView. – agapitocandemor Nov 25 '11 at 11:00
  • Here is, I found an app that has exactly what I need, a TabBar with different splitViews in each TabBarItem. And in the App Store!! That means that it is not so ilegal, or that there's a way to do it. [link](http://itunes.apple.com/us/app/english-russian-slovoed-deluxe/id287580812?mt=8) – agapitocandemor Nov 26 '11 at 10:26