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?

4

3 回答 3

5

问题是您ParentEndViewController在调用[storyboard instantiateViewControllerWithIdentifier:@"ParentEndViewController"];this 实例时实例化的新对象与根视图控制器的实例不同。如果您在应用程序委托中检查应用程序的根视图控制器,您应该尝试

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

如果您正在检查导航控制器的最后一个视图控制器,您应该尝试以下操作:

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");
 }
于 2013-04-29T11:20:01.933 回答
3

您可以通过window属性检查它:

if(viewController.view.window){

     // view visible

}else{

    // no visible

}
于 2013-07-17T10:03:25.323 回答
0

我猜当您签入 Appdelegate 时,ParentEndViewController 不是当前视图,因为该应用程序仍停留在加载过程中。

如果您将该代码放入 ParentEndViewController 的 viewDidAppear 中,您应该会得到正确的结果。

于 2013-04-29T10:47:33.920 回答