来自 AppDelegate 的 ViewController 中的预制函数 [英] Preform function inside ViewController from AppDelegate

查看:19
本文介绍了来自 AppDelegate 的 ViewController 中的预制函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AppDelegate 能否在收到推送通知时在视图控制器中运行函数"?

Can an AppDelegate run a "function" in a viewcontroller on receipt of a push notification?

该应用程序包含三个选项卡,当应用程序收到推送通知时,第三个选项卡需要重新加载其 webview.

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 {

}

推荐答案

是的,您需要为特定通知注册您的 viewController,例如,当您收到远程通知时,只需发布自定义通知并在您的 viewController 中注册此通知/收听此通知并执行您想要执行的方法.

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.

当您收到remoteNotification

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {

[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
}

在您的 viewController.m 中注册此通知的视图控制器

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];

}

然后实现选择器

-(void)pushNotificationReceived{

// do your stuff

}

最后不要忘记在您的 dealloc 方法中取消注册通知

and finally don't forget to unregister from notification in your dealloc method

-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

这篇关于来自 AppDelegate 的 ViewController 中的预制函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆