2

I am checking from my AppDelegate class whether my ParentEndViewController is currently the visible class or not.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil];  
ParentEndViewController *parent = [storyboard instantiateViewControllerWithIdentifier:@"ParentEndViewController"];
 if (parent.isViewLoaded && parent.view.window){
         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:displayName
                                                            message:body
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
        NSLog(@"current view is parent!");
    }
    else{
        NSLog(@"current view is not parent!");
    }

It is printing that current view is not parent!". But I am sure that the current view running on my app is ParentEndViewController, i.e it should print current view is parent!.

Where is the problem?

Steinar Lima
  • 7,644
  • 2
  • 39
  • 40
Leo
  • 135
  • 1
  • 2
  • 18
  • check this link:http://stackoverflow.com/questions/12807922/how-do-i-check-if-an-uiviewcontroller-is-currently-being-displayed – kirti Chavda Apr 29 '13 at 10:56

3 Answers3

5

The problem is that you instantiate a new object of ParentEndViewController when you call the [storyboard instantiateViewControllerWithIdentifier:@"ParentEndViewController"]; this instance is not the same as the instance of your root view controller. If you are checking the root view controller of your app in app delegate you should try

if([self.window.rootViewController isKindOfClass:[ParentEndViewController class]]) {
    NSLog(@"Luke I'm your father");
}
else {
    NSLog(@"Sorry bro, somebody else is the parent");
}

If you are checking the last view controller of your navigation controller you should try something like:

UIViewController *lastViewController = [[self.navigationController viewControllers] lastObject];

 if([lastViewController isKindOfClass:[ParentEndViewController class]) {
       NSLog(@"Luke I'm your father");
 }
 else {
     NSLog(@"Sorry bro, somebody else is the parent");
 }
bhavya kothari
  • 7,484
  • 4
  • 27
  • 53
danypata
  • 9,895
  • 1
  • 31
  • 44
  • Bro i am not checking the root view controller. I want check the view controller which is currently running. I tried with your lastViewController portion & its printing the log of else method! – Leo Apr 29 '13 at 11:33
  • In what method of your app delegate you are doing the check ? – danypata Apr 29 '13 at 11:39
  • into this method: "- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message". how can i catch the ParentEndViewController from AppDelegate to execute an action or functionality? – Leo Apr 29 '13 at 11:51
  • If you are using a UINavigationController as a root view controller of your app, you can use my last part of my answer but instead of using self.navigationController to get the nav controller, you should use: `UINavigationController *navController = (UINavigationController)self.window.rootViewController` after that you can get the last view controller of the `UINavigationController` stack by calling `[[navController viewControllers] objectAtIndex:[navController viewControllers].count - 1];`. I hope that this will help, it's hard to find a solution because I don't know the structure of your prj – danypata Apr 29 '13 at 12:11
  • bro i dont know how many times i have to thank u! its working now! u r awesome! :) – Leo Apr 29 '13 at 12:20
3

You can check it by window property:

if(viewController.view.window){

     // view visible

}else{

    // no visible

}
Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
Saad Ur Rehman
  • 798
  • 1
  • 10
  • 19
0

I guess when you are checking in the Appdelegate the ParentEndViewController is not the current view, because the app is still stuck in the loadingprocess.

If you put that code into the viewDidAppear of ParentEndViewController you should get the right outcome.

Amandir
  • 679
  • 1
  • 7
  • 20
  • Yes, u r right! so, can u suggest me how can i solve this? or how can i catch the ParentEndViewController from AppDelegate to execute an action or functionality? – Leo Apr 29 '13 at 11:16
  • sure, I just need to know what you want to achieve with this test. Why do you need to know if ParentEndViewController is the current viewController? – Amandir Apr 29 '13 at 11:20
  • I am receiving a message in AppDelegate class & if my current view is ParentEndViewController then i want to show the alert view with that message. – Leo Apr 29 '13 at 11:31