如何在UILabel中显示本地通知消息?

时间:2013-08-02 11:10:35

标签: ios

我需要在Label上显示本地通知消息。我知道应用程序运行时如何处理通知的语法。

在我的AppDelegate.m上这样,

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {

NSLog(@"Recieved Notification %@",notif);


}

看起来很好,我可以获取日志信息。 如何在AppDelegate中显示标签中的消息?像这样。

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {

NSLog(@"Recieved Notification %@",notif);

//Like this concept
MessageLabel.Text = FromNotificationMessage;

}

请帮帮我。我对iOS编程很感兴趣。怎么做?

3 个答案:

答案 0 :(得分:0)

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {

    NSLog(@"Recieved Notification %@",notif);

    UILabel * MessageLabel = [[UILabel alloc] init];
    MessageLabel.frame = CGRectMake(90, 10, 470, 57);
    MessageLabel.textAlignment = NSTextAlignmentCenter;
    MessageLabel.text = notif; // put your message String here.
    [self.window addSubview: MessageLabel];

}

答案 1 :(得分:0)

<{1>}在–application:didReceiveLocalNotification:中你可以发布下一个通知,你的每个类(观察者)都可以接收它;甚至是UIViewController类。

UIViewController课程中的任何一个都可以将文字放入UILabel或您想放置的任何位置。


<强> AppDelegate.m

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)note {

    NSString *_stringFromNotification = note.alertBody;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MyPersonalNotification" object:_stringFromNotification];

}

在您的UIVIewController班级中

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserverForName:@"MyPersonalNotification" object:nil queue:nil usingBlock:^(NSNotification *note) {
        NSString *_string = note.object;
        // ... do whatever you'd like to do with the string
    }];
}

注意:这只是可能的解决方案之一,而且非常简单地表达了这个想法。

答案 2 :(得分:0)

这取决于您希望在何处显示带有通知消息的标签。如果标签包含任何特定的UIView和相应的控制器,则需要在应用程序委托中获取控制器的引用。一旦你这样做,你需要通过控制器 - &gt;到达标签。查看 - &gt;子视图(标签)。现在,您可以在app delegate notification received方法中设置标签文本。

相关问题