ParentEndViewController

ParentEndViewController

我正在从AppDelegate类检查我的ParentEndViewController当前是否为可见类。

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!");
    }

它正在打印“current view is not parent!”。但是我确定我的应用程序上运行的当前视图是ParentEndViewController,即它应该打印current view is parent!

问题出在哪里?

最佳答案

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

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");
 }

08-19 12:30