Closed. This question needs details or clarity。它当前不接受答案。
想改善这个问题吗?添加详细信息,并通过editing this post阐明问题。
7年前关闭。
Improve this question
接收推送通知(didReceiveRemoteNotification)时,如何将通知处理从应用程序委托传递给视图控制器?
这对于在视图上显示警报很有用。释放视图或其他任何相关的信息。
AppDelegate.m的伪代码示例:
这段代码只是在所有内容之上抛出了模式视图。
或者,如果由于某种原因,您确实需要在不仅仅是应用程序委托的更多地方处理通知,那么您可以做两件事:
代表模型
在AppDelegate标头中创建一个新的委托协议,并将其设置为所需的任何处理程序-不利的一面(如上所述)是一次只能有一个对象可以收听委托
发布通知
您可以根据需要收听尽可能多的对象;在您想听的对象中:
并添加功能:
在您的应用程序委托回调上:
想改善这个问题吗?添加详细信息,并通过editing this post阐明问题。
7年前关闭。
Improve this question
接收推送通知(didReceiveRemoteNotification)时,如何将通知处理从应用程序委托传递给视图控制器?
这对于在视图上显示警报很有用。释放视图或其他任何相关的信息。
AppDelegate.m的伪代码示例:
- (void)application:(UIApplication*)application didReceiveRemoteNotification (NSDictionary*)userInfo{
NSLog(@"Received notification: %@", userInfo);
// Here send the userInfo or other data to the appropriate view controller for handling it there (for example showing an alert there) //
}
最佳答案
确实没有理由在应用程序周围传递didReceiveNotification。它打算处理一次;话虽这么说,我不太确定为什么要让一个代表来回通过。
如果您想将视图控制器推到最重要的位置(我对您的View层次结构一无所知,因此不知道这是否真的是您会使用的东西),则可以执行以下操作:
[[self.window rootViewController] presentViewController:[[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil] animated:YES completion:^{}];
这段代码只是在所有内容之上抛出了模式视图。
或者,如果由于某种原因,您确实需要在不仅仅是应用程序委托的更多地方处理通知,那么您可以做两件事:
代表模型
在AppDelegate标头中创建一个新的委托协议,并将其设置为所需的任何处理程序-不利的一面(如上所述)是一次只能有一个对象可以收听委托
@protocol MyNotificationDelegate <NSObject>
@required
-(void) applicationDidReceiveRemoteNotification: (NSDictionary*)userInfo;
@end
发布通知
您可以根据需要收听尽可能多的对象;在您想听的对象中:
AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ReceivedNotification" object:appDel];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationReceived:) name:@"ReceivedNotification" object:appDel];
并添加功能:
-(void)notificationReceived :(NSNotification *)localNot{
NSLog(@"userInfo from push: %@",localNot.userInfo );
}
在您的应用程序委托回调上:
- (void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo{
NSLog(@"Received notification: %@", userInfo);
[[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedNotification" object:self userInfo:userInfo];
}
关于ios - 如何在 View Controller (而不是AppDelegate)上处理远程通知,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17977377/