当前 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}
您可以使用 rootViewController
也当您的控制器不是 UINavigationController
:
UIViewController *vc = self.window.rootViewController;
一旦您知道根视图控制器,然后它取决于您如何构建您的 UI,但你能找到通过控制器层次结构中导航的方法。
如果你给一些定义您的应用程序的方式有关的更多详细信息,然后我可能会给一些更多的提示。
编辑:
如果你想要最上面的视图(而不是视图控制器),您可以检查
[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
虽然这种看法可能是不可见的或甚至所涵盖及其子视图的一些......
再次,它取决于您的 UI,但这可能帮助......
UIViewController *vc = [(UINavigationController *)self.window.rootViewController visibleViewController];
}
联系客服