簡體   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