1

I have an alert-style view that I want to push in front of whatever the current view is, but I need a reference to the current view so that I can call these two lines:

currentView.InsertSubviewAbove(myNewViewToShow, currentView);
currentView.BringSubviewToFront(myNewViewToShow);

So how do I get a reference to the current view?

I saw the answer at the bottom of this link: Get active view in iOS

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
UIView *topView = appDelegate.viewController.view;

and I tried this:

((AppDelegate)UIApplication.SharedApplication.Delegate).ViewController.View

but as far as I can tell, there is no 'ViewController' property in the Monotouch appDelegate.

I am using storyboards and segues to control my program flow. I have a background thread that is monitoring for various error conditions and needs to overlay a UIView on top of the currently-displayed view whenever an error occurs.

Community
  • 1
  • 1
nbonwit
  • 305
  • 4
  • 12

4 Answers4

2

It turns out that the solution was slightly more complicated, so I'm posting the full solution here for clarity's sake.

UIViewController currentController = UIApplication.SharedAppplication.KeyWindow.RootViewController;
while (currentController.PresentedViewController != null)
    currentController = currentController.PresentedViewController;

UIView currentView = currentController.View;

Basically, there is a tree of PresentedViewControllers and you have to walk to the end of the tree to see which one is on top. Then you just get the view for that controller.

Gautam Jain
  • 6,789
  • 10
  • 48
  • 67
nbonwit
  • 305
  • 4
  • 12
1

If your views are presented within a UINavigationController, from within any controller you can use:

this.NavigationController.TopViewController.View

If you are presenting something modally:

this.PresentedViewController.View

@stavash's approach works for other cases, however.

jonathanpeppers
  • 26,115
  • 21
  • 99
  • 182
  • Thanks for your suggestion, but I don't think I can use anything that uses 'this' because my background thread is not a view controller (see my question edit above)...any other ideas? – nbonwit Jun 23 '12 at 21:19
  • I was able to use your PresentedViewController.View concept with Miguel's RootViewController below. Thanks!! – nbonwit Jun 23 '12 at 21:41
1

The answer from "Get Native View in iOS" states that it depends on how your application is set up. In his sample he happens to have a handle to the topmost view controller in "viewController", so you would need to structure your code to be the same way for that to work.

With iOS 5, the recommended approach is to for your startup code to set the "RootViewController" property on your key window, so you can get access to your top view controller that way.

But in general, if you want to show an alert, you can make the API for your alert code take the current view controller as a parameter, something like:

void Alert (UIViewController myHost) {...}
miguel.de.icaza
  • 32,654
  • 6
  • 58
  • 76
  • Thanks for your answer. I think RootViewController is being set already (somehow) - I need to check. If I look at the RVC, how do I get access to the current view though? Is there a stack of view controllers that I can traverse/search? That's my issue...I don't know which view is currently being shown since I'm using storyboards and seques. – nbonwit Jun 23 '12 at 21:23
  • I got it to work by combining the RVC with Jonathan's PresentedViewController above. Thank you! – nbonwit Jun 23 '12 at 21:41
0

I would recommend creating a singleton class (maybe call it "ScreenManager") that it's sole purpose is to hold references to relevant views and manage their appearance. This way, every time you push/add a new view, you can hold a reference to it and then when you add this new alert view, you can do all the view-related actions by calling public methods on the ScreenManager. Hope this helps with getting you started.

Stavash
  • 14,244
  • 5
  • 52
  • 80
  • Thanks for your answer. Any idea on how to do that with storyboards/segues as that's what I'm using? – nbonwit Jun 23 '12 at 21:23
  • Sorry, I haven't had real experience yet with storyboards, though I still think it's worth the trouble as it is good MVC – Stavash Jun 24 '12 at 14:21