Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当前可见视图控制器检查_Ios_Iphone_Objective C_Uiviewcontroller - Fatal编程技术网

Ios 当前可见视图控制器检查

Ios 当前可见视图控制器检查,ios,iphone,objective-c,uiviewcontroller,Ios,Iphone,Objective C,Uiviewcontroller,我正在从我的AppDelegate类中检查我的ParentEndViewController当前是否为可见类 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil]; ParentEndViewController *parent = [storyboard instantiateViewControllerWithIdentifier:@"Parent

我正在从我的
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!");
    }
正在打印
当前视图不是父视图”。但我确信我的应用程序上运行的当前视图是
ParentEndViewController
,即它应该打印
当前视图是parent!


问题出在哪里?

我猜当您签入Appdelegate时,ParentEndViewController不是当前视图,因为应用程序仍处于加载过程中


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

问题是,当您调用
[storyboard InstanceDeviceController WithiIdentifier:@“ParentEndViewController”时,您实例化了一个新的
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");
 }

您可以通过
窗口
属性进行检查:

if(viewController.view.window){

     // view visible

}else{

    // no visible

}

检查此链接:是的,你是对的!那么,你能建议我如何解决这个问题吗?或者我如何从AppDelegate捕获ParentEndViewController以执行操作或功能?当然,我只需要知道你想通过此测试实现什么。为什么你需要知道ParentEndViewController是否是当前的viewController?我收到一条消息在AppDelegate类中&如果我的当前视图是ParentEndViewController,则我希望显示带有该消息的警报视图。Bro我没有检查根视图控制器。我希望检查当前正在运行的视图控制器。我尝试了lastViewController部分及其打印else方法的日志!在应用程序delegat的哪个方法中e您正在检查此方法:“-(void)xmppStream:(xmppStream*)发送方didReceiveMessage:(XMPPMessage*)消息“。如何从AppDelegate捕获ParentEndViewController以执行操作或功能?如果您使用UINavigationController作为应用程序的根视图控制器,则可以使用我回答的最后一部分,而不是使用self.navigationController获取导航控制器,您应该使用:
UINavigationController*navController=(UINavigationController)self.window.rootViewController
,然后您可以通过调用
[[navController viewControllers]objectAtIndex:[navController viewControllers].count获得
UINavigationController
堆栈的最后一个视图控制器。我希望这会有所帮助,很难找到解决方案,因为我不知道你的prjbro的结构我不知道我要感谢你多少次!现在开始工作了!你太棒了!:)