SimplyKiwi
SimplyKiwi

Reputation: 12444

visibleViewController without using UINavigationController?

In my app I am just using typical UIViewController methods to switch views. I am NOT using a UINavigationController. However, I really need to use the visibleViewController API that UINavigationController has.

No I cannot convert my project to using UINavigationController either. Is there any way to check if another UIViewController in my app is the controller that is currently visible?

I switch views using addSubview, and the hidden property right now. (I know this is not a proper way to switch views).

Anyway, in my situation, is it possible to do something similar to visibleViewController?

Thanks!

Upvotes: 1

Views: 709

Answers (2)

Son Nguyen
Son Nguyen

Reputation: 3481

There are 2 ways:

  1. Add sub views into a viewcontroller then use below method to bring one to front:

    [self.view bringSubviewToFront:aView];

  2. You can keep instance of view controllers in appDelegate, then set one of them is rootViewController using below code:

    self.window.rootViewController = self.viewController;

Upvotes: 0

vietstone
vietstone

Reputation: 9092

If you use addSubview:, there's only way to determine the top view in the view hierarchy. You can determine the top view by explore the subviews array property of the root view

UIView *topview = [[rootview subviews] lastObject];

If you want to get something similar to visibleViewController, you should store a map that keep the relationships between an UIView and an UIViewController. So you can get the visibleViewController by geting the topview firstly, then geting the corresponding view controller.

Good luck!

Upvotes: 2

Related Questions