当UINavigationController阻塞时访问UITableView(在UISplitViewController中)

时间:2012-03-08 02:45:04

标签: ios cocoa-touch ipad uitableview uisplitviewcontroller

我有一个像这样设置的UISplitViewController:

-(IBAction)makeStory:(id)sender{
NSLog(@"makeStory:");

    makeStoryTableViewController = [[MakeStoryTableViewController alloc] initWithNibName:@"MakeStoryTableViewController" bundle:nil];
    MakeSentenceTableViewController *detailViewController = [[MakeSentenceTableViewController alloc] initWithNibName:@"MakeSentenceTableViewController" bundle:nil];
    UISplitViewController *splitViewController = [[[UISplitViewController alloc] init] autorelease];

    UINavigationController *rootNav = [[[UINavigationController alloc] initWithRootViewController:makeStoryTableViewController]autorelease];

    UINavigationController *detailNav = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];

    splitViewController.viewControllers = [NSArray arrayWithObjects:rootNav, detailNav, nil];
    splitViewController.delegate        = makeStoryTableViewController;

    StoryBotAppDelegate *appDelegate = (StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.window setRootViewController:splitViewController];
}

当我尝试在didSelectRowAtIndexPath:的UISplitView委托中设置detailView时,我只能访问NavigationController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {   
    NSLog(@"ViewControllers: %@", [self.splitViewController viewControllers]);
    MakeSentenceTableViewController *detailViewController = [[self.splitViewController viewControllers] objectAtIndex:1];
    Story *storySet = [fetchedResultsController objectAtIndexPath:indexPath];
    NSLog(@"detailViewController: %@", detailViewController); //Logs a UINavigationController
    [detailViewController setStory:storySet]; //Fails here because Navigation Controllers Can't setStory!
    [detailViewController refreshTables];
} 

哪个有意义,但是如何访问UINavigationControllers负责的TableView?如果我删除* rootNav和* detailNav并将它们替换为splitViewController.viewControllers语句中的UITableViews它工作正常,但我没有导航栏:

    MakeSentenceTableViewController *detailViewController = [[MakeSentenceTableViewController alloc] initWithNibName:@"MakeSentenceTableViewController" bundle:nil];
    UISplitViewController *splitViewController = [[[UISplitViewController alloc] init] autorelease];

    //UINavigationController *rootNav = [[[UINavigationController alloc] initWithRootViewController:makeStoryTableViewController]autorelease];

    //UINavigationController *detailNav = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];

    splitViewController.viewControllers = [NSArray arrayWithObjects:makeStoryTableViewController, detailViewController, nil];
    splitViewController.delegate        = makeStoryTableViewController;

    StoryBotAppDelegate *appDelegate = (StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.window setRootViewController:splitViewController];

如何访问Detail TableView控制器并保留导航栏?

1 个答案:

答案 0 :(得分:0)

以下是我弄清楚的方法。我犯了两个错误,在那些错误中我不得不使用 UINavigationController.viewControllers ObjectAtIndex:属性。

  1. 在函数makeStory中,我错误地将UINavigationController指定为委托而不是makeStoryTableViewController。我通过这样做修复它,而是(特别注意splitViewController.delegate部分):

    MakeSentenceTableViewController *detailViewController = [[MakeSentenceTableViewController alloc] initWithNibName:@"MakeSentenceTableViewController" bundle:nil]; 
    UISplitViewController *splitViewController = [[[UISplitViewController alloc] init] autorelease];
    
    UINavigationController *rootNav = [[[UINavigationController alloc] initWithRootViewController:makeStoryTableViewController]autorelease];
    
    UINavigationController *detailNav = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
    
    splitViewController.viewControllers = [NSArray arrayWithObjects:rootNav, detailNav, nil];
    
    //splitViewController.viewControllers = [NSArray arrayWithObjects:makeStoryTableViewController, detailViewController, nil];
    //RootViewController *root = (RootViewController *)[navigationController.viewControllers objectAtIndex:0]
    
    splitViewController.delegate        = [rootNav.viewControllers objectAtIndex:0];
    
    NSLog(@"delegate: %@",  [rootNav.viewControllers objectAtIndex:0]);
    
    StoryBotAppDelegate *appDelegate = (StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSLog(@"RootViewController: %@", appDelegate.window);
    
    [appDelegate.window setRootViewController:splitViewController];
    
    NSLog(@"AppDelegate.window: %@", appDelegate.window.rootViewController);
    
  2. 我再次犯了同样的错误,这次是在didSelectRowAtIndexPath:并尝试在UINavigationController中设置setStory。为了解决这个问题,我做了这个(特别关注navControllerDetail.viewControllers:

    UINavigationController *navControllerDetail;
    navControllerDetail = [[self.splitViewController viewControllers] objectAtIndex:1];
    
    
    
    MakeSentenceTableViewController *detailViewController;
    
    detailViewController = [navControllerDetail.viewControllers objectAtIndex:0];
    
    Story *storySet = [fetchedResultsController objectAtIndexPath:indexPath];
    
    
    
    NSLog(@"detailViewController: %@", detailViewController);
    [detailViewController setStory:storySet];
    [detailViewController refreshTables];
    
  3. 现在,只要我能弄明白如何获得SplitViewController to rotate properly