如何在AppDelegate.m
的application:didReceiveRemoteNotification
方法中获取当前屏幕上的UIViewController
,以便响应来自APNs的推送通知并设置一些徽章视图?
我尝试使用self.window.rootViewController
获取当前显示的UIViewController
,它可能是一个UINavigationViewController
或其他类型的视图控制器。我发现可以使用UINavigationViewController
的visibleViewController
属性获取屏幕上的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
}