1

I'm using modal segue (without navigation controller) to move between viewController A and viewController B like so:

viewA *first = [self.storyboard instantiateViewControllerWithIdentifier:@"viewA"];
[self presentViewController:first animated:YES completion:nil];

And to move back :

 [self dismissViewControllerAnimated:YES completion:nil]; 

Now, I want to know from the AppDelegate whether A or B is the current view right now. The problem is when I'm checking

[(AppDelegate *)[[UIApplication sharedApplication] delegate] window]

the answer is always view A - the first one.

I've tried to set the current view every time I'm using modal segue like so:

viewA *first = [self.storyboard instantiateViewControllerWithIdentifier:@"viewA"];
    [self presentViewController:first animated:YES completion:^
{
[[(AppDelegate *)[[UIApplication sharedApplication] delegate] window] setRootViewController:first];
}];

But it cause a few bugs (like unable to use "dismissViewControllerAnimated"),and it's impossible to work like that in every segue in a big project with many segues.

How should I work with that? And how should I detect the current view in more appropriate way?

Asi Givati
  • 1,010
  • 1
  • 10
  • 25

2 Answers2

1

As was answered here

UIWindow *topWindow = [[[UIApplication sharedApplication].windows sortedArrayUsingComparator:^NSComparisonResult(UIWindow *win1, UIWindow *win2) {
    return win1.windowLevel - win2.windowLevel;
}] lastObject];

UIView *topView = [[topWindow subviews] lastObject];

However, doing this logic here sounds like bad architecture. What is the reason for you needing to know which view is currently presented inside of your AppDelegate?

Edit It seems like you want to respond to the applicationWillResignActive event from your view controller. Use something like this in the your game view controller.

- (void) applicationWillResign {
    NSLog(@"About to lose focus");
}

-(void) viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(applicationWillResign)
     name:UIApplicationWillResignActiveNotification
     object:NULL];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter]
     removeObserver:self];
}
Community
  • 1
  • 1
michaelsnowden
  • 5,532
  • 2
  • 32
  • 72
  • I have a game. The viewA is the main menu and viewB is the game itself. I want to pause the game every time the app is moving to background (applicationWillResignActive). so I need to know the current view to call the pause method or not (because if I'm at the main menu or something else I don't need to call the it). this is one of the examples. In all my apps I'm using only modal segue and when I need to call some methods from the appDelegate, I have to know what is the current view. This is how I work, but maybe it's wrong. So, if I'm doing something wrong I would like to know :) – Asi Givati Sep 07 '14 at 22:52
  • 1
    @AsiGivati That's a very real reason. A better solution is given [here](http://stackoverflow.com/questions/589598/how-to-react-to-applicationwillresignactive-from-anywhere). – michaelsnowden Sep 07 '14 at 23:04
  • Awesome! working perfect with NSNotificationCenter :) – Asi Givati Sep 07 '14 at 23:32
  • @AsiGivati [Also already answered, a lot of times](http://stackoverflow.com/questions/15656367/is-nsnotificationcenter-removeobserver-in-arc-needed). Consult google a little more before asking next time. – michaelsnowden Sep 07 '14 at 23:48
0

The appDelegate's window won't be equal to either view controller (viewControllerA or viewControllerB). You can ask the window for it's root view controller...

AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;

if (appDelegate.window.rootViewController == viewControllerA) {
    // always true if you always start the app with viewControllerA

... and you can ask any view controller for the view controller it presented...

if (appDelegate.window.rootViewController.presentedViewController == viewControllerB) {
   // will be true if viewControllerA has presented viewControllerB

But this is a tricky game. If, for example, viewControllerB presents some other viewControllerC, the condition above will continue to be true.

See the @Eric answer here (not the accepted answer) for a way to find the topmost vc in general.

Community
  • 1
  • 1
danh
  • 55,236
  • 10
  • 89
  • 124