当前可见视图控制器检查 [英] Current visible view controller checking
问题描述
我正在从我的AppDelegate
类中检查我的ParentEndViewController
当前是否是可见类.
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!");
}
它正在打印current view is not parent!
".但是我确定在我的应用程序上运行的当前视图是ParentEndViewController
,即它应该打印current view is 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!
.
问题出在哪里?
推荐答案
问题是您在调用[storyboard instantiateViewControllerWithIdentifier:@"ParentEndViewController"];
时实例化ParentEndViewController
的新对象,此实例与根视图的实例不同控制器.
如果要在应用程序委托中检查应用程序的根视图控制器,则应尝试
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");
}
这篇关于当前可见视图控制器检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!