繁体   English   中英

替换明细视图控制器

[英]Replacing Detail View Controller

我正在使用拆分视图控制器,并用新的视图控制器替换详细视图控制器时遇到问题。 启动应用程序时,我将空视图控制器作为详细视图控制器,然后单击导航栏上的按钮以显示主视图控制器。 当我在主视图控制器中选择一行时,详细视图控制器将替换为适当的控制器。 第一次选择行时它运行良好,但是每次之后,从主控制器中选择一行时,主控制器都不会消失,因此它只停留在细节控制器的顶部。

AppDelegate:

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

    // Override point for customization after application launch.

    MasterViewController *masterVC = [[MasterViewController alloc]initWithNibName:@"MasterViewController" bundle:nil];
    UINavigationController *masterNavController = [[UINavigationController alloc]initWithRootViewController:masterVC];

    DetailViewController *detailVC = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
    UINavigationController *detailNavController = [[UINavigationController alloc]initWithRootViewController:detailVC];

    masterVC.detailViewController = detailVC;

    self.splitViewController = [[UISplitViewController alloc]init];
    self.splitViewController.delegate = detailVC;
    self.splitViewController.viewControllers = @[masterNavController, detailNavController];

    self.window.rootViewController = self.splitViewController;

    [self.window makeKeyAndVisible];
    return YES;
}

MasterViewController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ActionAlertsViewController *rootView = [[ActionAlertsViewController alloc]initWithNibName:nil bundle:nil];
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    NSMutableArray *details = [self.splitViewController.viewControllers mutableCopy];

    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            rootView.title = @"Action Alerts";
            rootView.background = [UIImage imageNamed:@"capitol"];
            rootView.urlString = @"http://kyfbnewsroom.com/category/public-affairs/notifications/feed/";
            [rootView fetchEntries];

            UINavigationController *detailNav = [[UINavigationController alloc]initWithRootViewController:rootView];

            if (details.count > 1) {
                [details replaceObjectAtIndex:1 withObject:detailNav];
            } else {
                [details insertObject:detailNav atIndex:1];
            }

            appDelegate.splitViewController.viewControllers = details;
            appDelegate.window.rootViewController = self.splitViewController;
            appDelegate.splitViewController.delegate = rootView;
            [appDelegate.splitViewController viewWillAppear:YES];
        }
    }
}

我不知道从哪里开始:

  1. 不要复制/克隆UIViewControllers[self.splitViewController.viewControllers mutableCopy]
  2. 不要调用系统启动的消息: [appDelegate.splitViewController viewWillAppear:YES]
  3. 不要重置根视图控制器“ appDelegate.window.rootViewController
  4. 不要回到AppDelegate来找出您的视图
  5. @beyowulf指出:您不应该调用viewWillAppear ,它是系统调用的视图控制器生命周期方法。 您应该说类似[self.splitViewController showDetailViewController:detailNav sender:self]

请阅读文档并按照教程进行操作:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM