0
我正在使用分割視圖控制器,並在使用新視圖控制器替換我的詳細視圖控制器時出現問題。當我啓動應用程序時,我將我的空視圖控制器作爲我的詳細視圖控制器,並點擊導航欄上的按鈕以顯示主視圖控制器。當我在主視圖控制器中選擇一行時,我的詳細視圖控制器將被替換爲相應的控制器。在你第一次選擇一行時它可以正常工作,但是之後每次從主控制器中選擇一行時,主控制器都不會消失,因此它只停留在細節控制器之上。替換詳細視圖控制器
的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];
}
}
}
你不應該調用viewWillAppear中這是一個由系統調用的視圖控制器生命週期方法。你應該說[self.splitViewController showDetailViewController:detailNav sender:self]; – beyowulf
[self.splitViewController.viewControllers mutableCopy]? (a)不要以這種方式創建/複製視圖控制器。 (b)故事板? – SwiftArchitect