打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[ios]获取当前显示 UIViewController AppDelegate.m 在屏幕上

当前 UIViewController 屏幕需要从资深护师推式通知的回应,通过设置一些徽章的意见。但如何能 UIViewController 方法中 application:didReceiveRemoteNotification : 的 AppDelegate.m 吗?我试着使用 self.window.rootViewController 来获取当前显示的 UIViewController ,它可能是 UINavigationViewController 或某种其他类型的视图控制器。我发现, visibleViewController 属性的 UINavigationViewController 可以用于获取 UIViewController 在屏幕上。但如果不是我能怎么办 UINavigationViewController 吗?任何的帮助被感激 !相关的代码是作为以下。

AppDelegate.m

...- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {//I would like to find out which view controller is on the screen here.UIViewController *vc = [(UINavigationViewController *)self.window.rootViewController visibleViewController];[vc performSelector:@selector(handleThePushNotification:) withObject:userInfo];}...

ViewControllerA.m

-(void)handleThePushNotification:(NSDictionary *)userInfo{//set some badge view here}

解决方法 1:

您可以使用 rootViewController 也当您的控制器不是 UINavigationController

UIViewController *vc = self.window.rootViewController;

一旦您知道根视图控制器,然后它取决于您如何构建您的 UI,但你能找到通过控制器层次结构中导航的方法。

如果你给一些定义您的应用程序的方式有关的更多详细信息,然后我可能会给一些更多的提示。

编辑:

如果你想要最上面的视图(而不是视图控制器),您可以检查

[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];

虽然这种看法可能是不可见的或甚至所涵盖及其子视图的一些......

再次,它取决于您的 UI,但这可能帮助......

if ([self.window.rootViewController isKindOfClass:[UINavigationController class]]) {
  • UIViewController *vc = [(UINavigationController *)self.window.rootViewController visibleViewController];
    }

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
iOS与Unity3d的交互实现
iPhone开发之NavigationController
Storyboard多View的切换 [xcode 4.4.1]
visibleViewController和topViewController获取当前显示的页面
iOS App的生命周期
UIViewController使用技巧!
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服