Tutorial :Substitution of detail view controller does not cause viewWillAppear to be called


I'm porting my iPhone app to iPad. On iPhone I select row in the table, and after that the next view controller is pushed to the top of navigationController (now navigation is performed on the left part of split view controller). For iPad i modified the code this way:

if (deviceIsIPad())  {      UISplitViewController *svc = (UISplitViewController *)[self findNearestParentOfClass:[UISplitViewController class]];      svc.viewControllers = [NSArray arrayWithObjects:[svc.viewControllers objectAtIndex:0],                                                       nextViewController,                                                       nil];  }  else      [self.navigationController pushViewController:nextViewController animated:YES];  

There are no problem at iPhone code (when controller is pushed to navigation controller), but on iPad viewWillAppear: is not called (viewDidLoad is called however), while I have a reason to perform some customization right in viewWillAppear:. Why is it not called, and what should I do to force it to be called?

Much thanks in advance!


Not exactly sure what you're trying to do here but simply initializing a splitview controller and adding controllers to it does not force views to appear. You have to add the splitview controller's views to the window.

Here is the code from the Xcode Splitview template's app delegate's applicationDidFinishLaunching:

splitViewController = [[UISplitViewController alloc] init];  splitViewController.viewControllers = [NSArray arrayWithObjects:navigationController, detailViewController, nil];  splitViewController.delegate = detailViewController;    NSLog(@"master=%@",splitViewController.viewControllers);  // Add the split view controller's view to the window and display.  [window addSubview:splitViewController.view];  [window makeKeyAndVisible];  

The views in the navigation controller appear because it is already attached to the window and displaying the view of one of its controlled controllers.


From Comments:

All initialization code you are quoted here is already performed. Now, let's assume one have a table on the left controller, then he select another row there, and want to replace right controller with another one. splitViewController.view is added on the window фдкуфвн, because some GUI elements initialized in viewDidLoad, are properly presented on view

It sounds like your problem arises because the detail (right-side) view of a splitview controller is always visible i.e. it only appears i.e calls viewWillAppear, once immediately after first being loaded. There is no point at which the detail side has no view present. I'm not sure what entirely swapping out viewControllers of the splitview will do.

If you want to change the detail view on the fly. You need to put a navigation controller in the right side and then push and pop view-controllers in that nav in response to events in the right side controller.

Look at how the iPad iPod app works. You have a leftside view of playlist and on the right side, a list of all the songs in the playlist. Selecting a song pushes a song detail view on top the list of songs.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »