Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewController viewDidAppear - because it was pushed or because subview was popped?

In my app, I have about a dozen different view controllers, pushed onto or popped from the stack in different orders. When a view has appeared (I'm using viewDidAppear method) I need to determine whether this is because this view has just been pushed onto the stack or because one of its subviews was popped from the stack.

I went through this question: viewWillDisappear: Determine whether view controller is being popped or is showing a sub-view controller

But I don't think it's helping me very much. Any suggestions please?

like image 203
Aleks G Avatar asked Dec 21 '11 14:12

Aleks G


1 Answers

The best solution, if you can manage it, is to try to revise your code so that it doesn't matter whether the controller was just pushed or its child was just popped. Fundamentally, a view controller mediates between its view and the data that the app operates on. As long as that data is up to date, the controller shouldn't be concerned with what was happening before its view appeared. Tasks that your controller currently does based on the previous state of the app, such as updating the data, might really be better located in a different class.

Another possibility, if you're using storyboards, is to rely on -prepareForSegue:sender: instead of -viewDidAppear. The segue you're passed in that method has properties that identify the source and destination view controllers, and that's usually enough information to tell you how your controller came to be the current one.

If neither of those work in your case, consider moving your configuration code to one or more different methods. The root of the problem you're facing is that -viewWillAppear really doesn't mean what you need it to. Create a method that does mean what you need, like -childControllerFinished, and use that to do the configuration work that you need.

like image 108
Caleb Avatar answered Oct 04 '22 02:10

Caleb