从AppDelegate中里面的ViewController preform功能 [英] Preform function inside ViewController from AppDelegate
问题描述
能否在AppDelegate中收到一个推送通知的运行在一个的ViewController功能?
该应用程序包含三个选项卡,第三个需要时,应用程序获取推送通知重新加载其网页视图。
- (无效)应用:(*的UIApplication)的应用
didReceiveRemoteNotification:(NSDictionary的*){用户信息}
是的,你需要注册你的的viewController
为特定的通知,例如,在当你收到你的情况远程通知,只是发布自定义通知,并在您的viewController,注册该通知/收听该通知并执行要执行的方法。
当您收到 remoteNotification
- (无效)应用:(*的UIApplication)的应用
didReceiveRemoteNotification:(NSDictionary的*){用户信息[NSNotificationCenter defaultCenter] postNotificationName:@pushNotification的对象:无用户信息:用户信息];
}
在 viewController.m
注册的ViewController此通知
- (无效)viewDidLoad中{ [NSNotificationCenter defaultCenter]的addObserver:自我选择:@selector(pushNotificationReceived)名称:@pushNotification的对象:无]; [超级viewDidLoad中];}
和下一个执行选择
- (无效){pushNotificationReceived//做你的东西}
最后不要忘了,从通知你的注销的dealloc
法
- (空)dealloc的{
[NSNotificationCenter defaultCenter] removeObserver:个体经营];
}
Can an AppDelegate run a "function" in a viewcontroller on receipt of a push notification?
The application contains three tabs, the third needs to reload its webview when the application gets a push notification.
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
}
yes, you need to register your viewController
for the particular notification, e.g in your case when you receive a remote notification, just post a custom notification and in your viewController, register this notification/listen to this notification and perform the method you want to perform.
when you receive remoteNotification
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
}
in your viewController.m
register the viewcontroller for this notification
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushNotificationReceived) name:@"pushNotification" object:nil];
[super viewDidLoad];
}
and next implement the selector
-(void)pushNotificationReceived{
// do your stuff
}
and finally don't forget to unregister from notification in your dealloc
method
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
这篇关于从AppDelegate中里面的ViewController preform功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!