iOS如何判断当前展示窗口(即具体在那个视图控制器VC)?
- (UIViewController *)activityViewController
{
UIViewController* activityViewController = nil;
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
if(window.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow *tmpWin in windows)
{
if(tmpWin.windowLevel == UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
}
NSArray *viewsArray = [window subviews];
if([viewsArray count] > 0)
{
UIView *frontView = [viewsArray objectAtIndex:0];
id nextResponder = [frontView nextResponder];
if([nextResponder isKindOfClass:[UIViewController class]])
{
activityViewController = nextResponder;
}
else
{
activityViewController = window.rootViewController;
}
}
return activityViewController;
}
判断当前展示的是否我指定的某个控制器?
当前窗口可以为当前打开的任意界面,此时我要判断是否打开的界面是我的烟雾探测器执行界面。
UIViewController *vc = [self activityViewController];
ANSSmogAlarmViewController *smogVC =[[ANSSmogAlarmViewController alloc] init];
if ([vc isMemberOfClass:[smogVC class]])
{
NSLog(@"点击了确定健");
dispatch_async(dispatch_get_main_queue(), ^{
smogVC.smogAlarmStateImageView.image = [ImageCenter imageWithName:@"device_smog_alarm_image_highlight@2x"];
});
}