最佳答案
屏幕上当前的 UIViewController需要通过设置一些徽章视图来响应来自 APN 的推送通知。但是我怎样才能在方法 application:didReceiveRemoteNotification: 的 AppDelegate.m中得到 UIViewController呢?
我尝试使用 self.window.rootViewController得到当前显示的 UIViewController,它可能是一个 UINavigationViewController或其他类型的视图控制器。我发现 UINavigationViewController的 visibleViewController属性可以用来获得屏幕上的 UIViewController。但是如果它不是 UINavigationViewController我该怎么办?
感谢您的帮助! 相关代码如下。
应用代表
...
- (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
}