gpt4 book ai didi

ios - 当 UINavigationController 妨碍时访问 UITableView(在 UISplitViewController 中)

转载 作者:行者123 更新时间:2023-11-28 17:36:43 27 4
gpt4 key购买 nike

我有一个像这样设置的 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];
}

当我尝试在 UISplitView 委托(delegate)中设置 detailView 作为 didSelectRowAtIndexPath: 的结果时,我只能访问 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];
}

这是有道理的,但我如何访问 UINavigationController 负责的 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 Controller 并保留导航栏?

最佳答案

我是这样想的。我犯了两个错误,在这些错误中我不得不使用 UINavigationController.viewControllers ObjectAtIndex: 属性。

  1. 在函数 makeStory 中,我错误地将 UINavigationController 指定为委托(delegate)而不是 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 中设置故事。为了修复它,我这样做了(特别注意关于 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];

现在要是我能弄清楚如何获得 SplitViewController to rotate properly 就好了!

关于ios - 当 UINavigationController 妨碍时访问 UITableView(在 UISplitViewController 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9612231/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com