Objective c 在Appdelegate中传递变量
当我在Objective c 在Appdelegate中传递变量,objective-c,variables,appdelegate,Objective C,Variables,Appdelegate,当我在AppDelegate中使用变量,然后在函数中使用该变量时,该变量不会更改其值 有什么帮助吗?我使用目标c - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // Show alert for push notifications recevied while the // app is running
AppDelegate
中使用变量,然后在函数中使用该变量时,该变量不会更改其值
有什么帮助吗?我使用目标c
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// Show alert for push notifications recevied while the
// app is running
NSString *message = [[userInfo objectForKey:@"aps"]
objectForKey:@"alert"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@""
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
如何获取此函数之外的消息?您可以创建一个类变量:
NSString *mesage;//You can use this variable anywhere in AppDelegate
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// Show alert for push notifications recevied while the
// app is running
message = [[userInfo objectForKey:@"aps"]
objectForKey:@"alert"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@""
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
您是否考虑过将消息存储在SharedReferences中,以便在不引用AppDelegate的情况下在任何位置检索它?这可能是一个比在函数外部引用和强制转换AppDelegate.declare变量更干净的解决方案Martino Lessio我怎么做?我想在appdelegate中使用它,而不是对其他ViewControllers调用“DidReceiveMemoteNotification”之前,变量消息为空。我在xcode中发送了一个推送和打开的日志,并且得到了一个空值。您确定“[[userInfo objectForKey:@“aps”]objectForKey:@“alert”];”不为空?以下是我所做的:从xcode运行应用程序到我的设备,打开应用程序,关闭应用程序,然后发送推送,在我的屏幕上,我得到推送,打开应用程序,在功能中变为活动状态。我已经输入了nslog消息,我得到了空值。投票给这个答案的人能帮助我吗?