【问题标题】:Passing variables inside Appdelegate在 Appdelegate 中传递变量
【发布时间】:2016-04-12 09:36:31
【问题描述】:

当我在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];

如何在此函数之外获取消息?

【问题讨论】:

  • 您是否考虑过将消息存储在 SharedPreferences 中,以便在不引用 AppDelegate 的情况下在任何地方检索它?这可能是比引用和强制转换 AppDelegate 更清洁的解决方案。
  • 在外部函数中声明变量
  • Martino Lessio 我该怎么做?我想在 appdelegate 中使用它而不是其他视图控制器

标签: objective-c variables appdelegate


【解决方案1】:

你可以创建一个类变量:

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

【讨论】:

  • 在调用“didReceiveRemoteNotification”之前变量消息为空
  • 我发送了一个推送并在 xcode 中打开了日志,但我得到了一个 null
  • 你确定 "[[userInfo objectForKey:@"aps"] objectForKey:@"alert"];"不为空?
  • 这就是我所做的:从 xcode 运行应用程序到我的设备,打开应用程序,关闭应用程序然后发送推送,在我的屏幕上我得到推送打开应用程序和功能 didbecomeactive 我有输入 nslog 消息,我得到 null
  • 投票此答案为有帮助的人也可以帮助我吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-10
  • 2013-12-25
  • 2017-05-22
  • 2020-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多