viewWillAppear only being called once

1.2k views Asked by At

Here's the scenario, switchViewController is the view added to the main window. So switchViewController is the main view, so if I want to go view B, I will addsubview of view B, there isn't a need to remove switchViewController's view right? The issue is after I go back from view B to switchViewController's view, the method viewWillAppear is not being called anymore.

Why is it so?

3

There are 3 answers

0
Jim On

That's because it never disappeared, you were just putting something else in front of it. If you want to navigate from one screen to another and back, they should be separate view controllers, and you should be using UINavigationController and its pushViewController:isAnimated: method.

0
Ole Begemann On

viewWillAppear: is not called automatically when a view is removed from or added to the view hierarchy. It is the responsibility of the view controller to call it at the right time. The built-in view controller classes do this whenever you present or push a new view controller. Since you do not use this mechanism in your app, the method doesn't get called (unless you call it yourself).

0
DVG On

It's not getting called beause your just modifying the first view, not navigating to a different one.

You might consider embedding your view in a Navigation controller, then calling your ViewB with

[navigationController pushViewController:viewB animated:YES];