如何处理来自应用程序委托的 UIViewcontroller 中的通知?
How to handle notification in UIViewcontroller from app delegate?
我收到通知 appdelegate didReceiveRemoteNotification
但我想在视图控制器中使用此有效负载数据如何操作?
您必须将 UITabBarController 放在 AppDelegate 的 UINavigationController 中。完成此操作后,您就可以将应用程序主 window 的 rootViewController 声明为 UINavigationController。在 AppDelegate 的头文件中声明:
@property (nonatomic, strong) UINavigationController * mainWindowRootViewController;
然后这样做:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[_window makeKeyAndVisible];
_mainWindowRootViewController = [UINavigationController new];
[self.window setRootViewController:_mainWindowRootViewController];
///code code code
[_mainWindowRootViewController setViewControllers:@[splashScreen, tabBarController] animated:TRUE];
...//code code code
return YES;
}
现在您的 mainWindowRootViewController 将 splashScreen 作为它的 rootViewController 并将 tabBarController 作为堆栈中的第二个 viewcontroller,您可以这样做:
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
UIViewController * commentsViewController = [UIViewController new];
UINavigationController * tempNAV = [[UINavigationController alloc] initWithRootViewController:commentsViewController];
[_mainWindowRootViewController presentViewController:tempNAV animated:TRUE completion:nil];
}
当您呈现评论视图控制器时,它将呈现在整个控制器堆栈上,包括 UITabBarController 和 splashScreen(如果您有启动画面)。另外,对于所有可能会说 "you can't present a UINavigationController with a UINavigationController" 的人,是的,你可以自己尝试一下,太棒了!
这假设您在 AppDelegate 中声明了评论视图控制器,如果您在其他地方声明了这个 "localNotif",那么您可以在整个应用程序的整个视图控制器堆栈上执行相同的演示 window 通过使用以下修改:
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
UIViewController * commentsViewController = [UIViewController new];
UINavigationController * tempNAV = [[UINavigationController alloc] initWithRootViewController:commentsViewController];
[[(YOURAppDelegate *)[UIApplication sharedApplication].delegate mainWindowRootViewController] presentViewController:tempNAV animated:TRUE completion:nil];
}
实现这个的方法有很多,问题有点含糊。假设您的应用程序中有一个视图控制器,当您的通知到达时它处于活动状态,也许最简单的方法是使用 NSNotification 将有效负载从应用程序委托广播到您感兴趣的视图控制器。
在您的视图控制器中:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receivedRemoteNotification:)
name:@"RemoteNotification"
object:nil];
并实现方法-receviedRemoteNotfication
。
然后,在您的应用委托的远程通知方法中:
[[NSNotificationCenter defaultCenter]
postNotificationName:@"RemoteNotification"
object:payload];
我收到通知 appdelegate didReceiveRemoteNotification
但我想在视图控制器中使用此有效负载数据如何操作?
您必须将 UITabBarController 放在 AppDelegate 的 UINavigationController 中。完成此操作后,您就可以将应用程序主 window 的 rootViewController 声明为 UINavigationController。在 AppDelegate 的头文件中声明:
@property (nonatomic, strong) UINavigationController * mainWindowRootViewController;
然后这样做:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[_window makeKeyAndVisible];
_mainWindowRootViewController = [UINavigationController new];
[self.window setRootViewController:_mainWindowRootViewController];
///code code code
[_mainWindowRootViewController setViewControllers:@[splashScreen, tabBarController] animated:TRUE];
...//code code code
return YES;
}
现在您的 mainWindowRootViewController 将 splashScreen 作为它的 rootViewController 并将 tabBarController 作为堆栈中的第二个 viewcontroller,您可以这样做:
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
UIViewController * commentsViewController = [UIViewController new];
UINavigationController * tempNAV = [[UINavigationController alloc] initWithRootViewController:commentsViewController];
[_mainWindowRootViewController presentViewController:tempNAV animated:TRUE completion:nil];
}
当您呈现评论视图控制器时,它将呈现在整个控制器堆栈上,包括 UITabBarController 和 splashScreen(如果您有启动画面)。另外,对于所有可能会说 "you can't present a UINavigationController with a UINavigationController" 的人,是的,你可以自己尝试一下,太棒了!
这假设您在 AppDelegate 中声明了评论视图控制器,如果您在其他地方声明了这个 "localNotif",那么您可以在整个应用程序的整个视图控制器堆栈上执行相同的演示 window 通过使用以下修改:
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
UIViewController * commentsViewController = [UIViewController new];
UINavigationController * tempNAV = [[UINavigationController alloc] initWithRootViewController:commentsViewController];
[[(YOURAppDelegate *)[UIApplication sharedApplication].delegate mainWindowRootViewController] presentViewController:tempNAV animated:TRUE completion:nil];
}
实现这个的方法有很多,问题有点含糊。假设您的应用程序中有一个视图控制器,当您的通知到达时它处于活动状态,也许最简单的方法是使用 NSNotification 将有效负载从应用程序委托广播到您感兴趣的视图控制器。
在您的视图控制器中:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receivedRemoteNotification:)
name:@"RemoteNotification"
object:nil];
并实现方法-receviedRemoteNotfication
。
然后,在您的应用委托的远程通知方法中:
[[NSNotificationCenter defaultCenter]
postNotificationName:@"RemoteNotification"
object:payload];