简体   繁体   中英

Replacing Detail View Controller

I am using a split view controller and have an issue when replacing my detail view controller with a new view controller. When I start the app I get my empty view controller as my detail view controller and hit the button on the navigation bar to show the master view controller. When I select a row in master view controller my detail view controller is replaced with the appropriate controller. It works fine the first time you select a row, but every time after that, when a row is selected from the master controller, the master controller doesn't go away so it just stays on top of the detail 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];
        }
    }
}

I don't know where to start:

  1. Don't copy/clone UIViewControllers : [self.splitViewController.viewControllers mutableCopy]
  2. Don't invoke system-initiated messages: [appDelegate.splitViewController viewWillAppear:YES]
  3. Don't reset the root view controller" appDelegate.window.rootViewController
  4. Don't go back to the AppDelegate to find out which view you are
  5. @beyowulf noted: You shouldn't be calling viewWillAppear that is a view controller lifecycle method that is called by the system. You should say something like [self.splitViewController showDetailViewController:detailNav sender:self]

Do read the documentation and follow the tutorials:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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