问题描述
- AppDelegate.m中的局部产量在viewcontroller.m中如何调用?
-
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// 打印到日志 textView 中
[self.viewController addLogString:[NSString stringWithFormat:@"backgroud : %@",userInfo]];completionHandler(UIBackgroundFetchResultNewData);
}
以上代码为AppDelegate.m中的userInfo .这个userInfo是不是局部产量?我需要在viewcontroller.m中调用它,比如说把它拼接到字符串里面,我该使用什么方法呢? - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
解决方案
使用NSNotificationCenter
1.首先,在viewcontroller.m中注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getDict:) name:@"test" object:nil];
并且定义通知触发之后的动作getString函数
- (void)getDict:(NSNotification*)notification{
NSDictionary *dict=[notification object];
NSLog(@"userInfo:%@",dict);
//对传过来的值进行操作
}
2.在AppDelegate.m中触发通知
(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// 打印到日志 textView 中
[self.viewController addLogString:[NSString stringWithFormat:@"backgroud : %@",userInfo]];
//触发通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
你试试,不行的话再找我。