UIViewController viewDidAppear - because it was clicked or because a subview was displayed?

In my application, I have about a dozen different view controllers that are popped out or popped out of it in different orders. When the view has appeared (I use the viewDidAppear method), I need to determine whether this is due to the fact that this view was just pushed onto the stack or because one of its subzones was pushed from its stack.

I reviewed this question: viewWillDisappear: determine if a view controller is displayed or a subtask controller is displayed

But I donโ€™t think it helped me much. Any suggestions please?

+6
source share
3 answers

What you can do is have a BOOL property on your view controller, called already pressed or something similar. Then, in your viewDidAppear method, check if it is false (which will be called viewDidAppear for the first time) if it is set to true.

Then, when the popview viewController pops up, the viewDidAppear method checks to see if the property is already set to true if you know that the preview has just appeared.

+1
source

The best solution, if you can control it, is to try to review your code so that it doesn't matter if only the controller is pressed or its child just popped up. In essence, the view controller mediates between its view and the data that the application is running. While this data is being updated, the controller does not have to worry about what happened before its appearance. The tasks that your controller currently does based on the previous state of the application, such as updating data, may be better located in another class.

Another possibility, if you are using storyboards, is to rely on -prepareForSegue:sender: instead of -viewDidAppear . The segment that you passed in this method has properties that the source and destination view controllers define, and this is usually enough information to tell you how your controller has become current.

If none of these work in your case, consider porting the configuration code to one or more different methods. The root of the problem you are facing is that -viewWillAppear doesnโ€™t really mean what you need. Create a method that means what you need, such as -childControllerFinished , and use this to do the necessary configuration work.

+4
source

If the view controller is viewDidLoad stack, the viewDidLoad method will be called; if its sub-controller popped up, then viewDidLoad NOT called.

 - (void)viewDidLoad { [super viewDidLoad]; //do any setup you want to do only when the view is pushed onto the //stack for the first time; this method will not be called //when the view subview is popped and this one becomes visible } 

So, use viewDidLoad (or initWithNibName , depending on what you are doing) for a one-time setup when the view controller is pressed for the first time, and viewDidAppear for the setup you want to make each time the view appears. Using the UINavigationController, you can implement these two methods in the UINavigationControllerDelegate protocol:

 - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { [viewController viewWillAppear:animated]; //do any every-time setup *before* the view is shown } - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { [viewController viewDidAppear:animated]; //do any every-time setup *after* the view is shown } 
0
source

Source: https://habr.com/ru/post/904246/


All Articles