在Appdelegate中传递变量

时间:2016-04-12 09:36:31

标签: objective-c variables appdelegate

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

如何在此功能之外获取消息?

1 个答案:

答案 0 :(得分:2)

您可以创建一个类变量:

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