How to determine which view controller is currently active/the one displaying a view? - ios

In my app I am queueing some local notifications, when they fire I must present a modal view. The trouble is I have numerous view controllers any one of which could currently be active and thus the one that needs to present the modal view controller. How can I determine which one is currently in use?
I am setting a navigation controller as the windows root view controller, and this can push any number of other view controllers, some of them themselves may also be currently presenting another view controller modally.
This must work on iOS 4 and 5.
I have a lot of view controllers so would like to avoid putting code in each of them to each check if they are currently the top one.

You can look at the navigation controller's topViewController property to find out which controller is at the top of the stack. This will be the one whose view is displayed.
Since you may also be presenting a modal view controller, you'll probably be more interested in the visibleViewController property, which will give you the controller for the current view whether its presented modally or pushed onto the navigation stack.

Create a variable that stores a pointer to the ViewController that was most recently pushed. Every time you push a new ViewController, update this variable. Then you'll always know which one is on the top!

Related

What is the best way to present a view controller on top of everything in iOS?

I'm trying to present a view controller that will be kept displayed above everything, regardless of the currently presented view controller, so it will be kept as displayed even if the view controller behind it will get dismissed, or starts to present another view controller.
I know how to find the topmost view controller (for example as suggested here) but in this case presenting over this view controller will make the new view controller depends on the hierarchy of the parent controller, which I try to avoid.
Present the view from the rearmost view, the window. But give it a high z index so that it shows up in front of everything else.

Know all the views presented on a view controller

I am facing a problem where I have multiple views presented at a particular time. Now if my app get timed out and I have to get back to Login Screen, I am able to pop all view controllers from the navigation stack but how to Know how many view controllers are presented at that particular moment so that I can dismiss all of them.
navigationController.viewControllers would return the current view controller stack. try that.

What is the proper way to remove a UIViewController from the background memory when using instantiateViewControllerWithIdentifier?

I am presenting a view controller like this:
UINavigationController * PlacesNC = [storyboard instantiateViewControllerWithIdentifier:#"PlacesNavigationController"];
[self presentViewController:PlacesNC animated:YES completion:nil];
Once I am at the final view controller that is presented using the navigation view controller, how can I remove the initial view controller from memory?
Edit
In my application it is possible to present multiple UIViewControllers in this fashion. I will try to give a representation of what my stack might look like.
____ modal vc 5 * current view controller
____ modal vc 4
____ modal vc 3
____ modal vc 2
____ modal vc 1
____ root view controller
So if I have a stack like this, how can I remove vc 1,2,3 and maybe even 4 so that I can release the memory these other view controllers are using up? This may not be the correct to allow such a stack as this, but this is what I have now, and I just need a temporary fix as the iPhone 4 cannot handle the amount of memory used in a stack like this. So until I can come up with an alternate way to present my view controllers I just need to be able to remove some of them from the stack.
You cannot, and you don't want to. It is still in the view controller hierarchy, as I show in this diagram (DrillViewController is presented, but RootViewController is still there, "behind" it):
Presenting a view controller does not destroy the presented view controller, nor should it; if it did, you would not be able to dismiss the presented view controller and find the presenting view controller still sitting there.
Moreover, in iOS 8 it is perfectly possible to present a view controller and show the presented view controller's view in front of the presenting view controller's view, which remains visible behind it. That, for example, is how a UIAlertController presentation works. Clearly it would be a disaster if the presenting view controller ceased to exist in that situation.
Now, it may be that what you really mean is: My view controller has a property that uses a lot of memory. I don't need to hang on to that when my view controller is not frontmost. So I'd like to release it when I present another view controller. In that case, just manage the memory manually: set that property to nil on viewDidDisappear:, and recover its value (somehow) on viewWillAppear: when the presented view controller is dismissed.
One final suggestion: Perhaps the real problem is that a presented view controller was just the wrong type of hierarchical arrangement to start with. Perhaps what you really want to do is replace your original view controller in the view controller with the new view controller - because you are never coming back to it and don't need it any longer. That is perfectly possible, but of course you'll need to set up a different hierarchy architecture.

Get a reference to the currently visible view controller

I have a UIAlertView. When the user taps a button in the alert view I want to show a new UIViewController.
In order to achieve this I need to know which view controller is currently visible on screen because that particular view controller is the right one to present the new view controller.
The problem is that I have a complex hierarchy of view controllers in my app including a UINavigationController and a UITabBarController (among others). So I cannot simply use self.visibleViewController to get the currently visible view controller.
I have found a possible solution on Stackoverflow but I would like to find a neater solution without having to dig through the whole view controller stack.
UINavigationController has a property called topViewController.
Maybe it helps you.

iOS View Controller Containment Parent/Child calls dance

I am trying to make a container view controller that works similarly to navigation controller. When I add something to the stack what do I do with the view controller that is a already there?
It is still my child but I don't want it's view in the view hierarchy. Should I call removeFromParentViewController on it, and just keep a separate stack with it, in that stack? So when the view above is popped off, I can check what view I should push back in order to go back to previous one.
Or should I just remove its view, without removeFromParentViewController call, and add another child controller, and its view to container view hierarchy?
Basically what do I do with the controllers that aren't on the screen?
The "stack" is just an array that a navigation controller uses to keep track of its view controllers. If you're building your own, you will need to have an array also. The way a navigation controller works, when a controller is pushed, that controller is added to the array, and if one is popped, that one is removed from the array. When you do a transition, the one that's going off screen should call removeFromParentViewController, so it's no longer in the hierarchy (but if it's going away because of another one being pushed, you would leave it in your array -- that's how the controller knows which one to go back to on a pop). You should use transitionFromViewController:toViewController:duration:options:animations:completion: to do your transitions from one controller to the next.

Resources