prepareForSegue is not called after performSegue:withIdentifier: with popover style

南楼画角 提交于 2019-11-28 06:35:38

What I have discovered so far, is that with any segue identifier that is not popover these are the invocations made by iOS:

  • prepareForSegue (on source controller)
  • viewDidLoad (on destination controller)

while in popover segue the invocation order is:

  • viewDidLoad (on destination controller)
  • prepareForSegue (on source controller)

just because I put all my logic in viewDidLoad, the controller was not properly initialized, and a crash happened. So this is not exactly true that prepareForSegue is not called, the truth is that I was getting an exception, and I wrongly mistaken as prepareForSegue not getting called.

I couldn't put everything in viewWillAppear because a call to CoreData had to be made and I didn't want to check if entities were ok each time the view display.

How did I solve this ? I created another method in destination controller

-(void)prepareViewController {
  // initialization logic...
}

and changing the prepareForSegue method in source controller itself:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  MyViewController *mvc = (MyViewController*)[segue destinationViewController];
  // passing variable 
  // with segue style other than popover this called first than viewDidLoad
  mvc.myProp1=@"prop1"; 
  mvc.myProp2=@"prop2";

  // viewWillAppear is not yet called
  // so by sending message to controller
  // the view is initialized
  [mvc prepareViewController];

}

don't know if this is expected behavior with popover, anyway now things are working.

I've noticed that the boiler plate code for Xcode's Master-Detail template (iPhone) uses the following pattern for configuring the detail VC's view:

  1. the detail VC's setters (for properties) are overwritten in order to invoke the configureView method (configureView would update all your controls in the view, e.g., labels, etc.)
  2. the detail VC's viewDidLoad method also invokes the configureView method

I did not follow this pattern the other day when I was trying to re-use a detail VC in my movie app, and this gave me trouble.

I don't have much experience with popovers; however, if the pattern above is used with a detail VC that is displayed inside a popover, then wouldn't the detail VC's view get configured when you set the detail VC's properties from within the prepareForSegue method?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!