如何向iOS用户发送即时本地通知?

时间:2012-07-18 03:16:12

标签: objective-c ios xcode cocoa-touch

我只想在用户按下按钮时弹出通知。无需服务器或计时器。我能找到的所有教程似乎都涉及这两个教程中的一个。

3 个答案:

答案 0 :(得分:1)


UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelMessage];
[alertView show];

答案 1 :(得分:1)

也许是这样的:

-(IBAction) buttonPressed: (UIButton *)sender{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: [NSString stringWithFormat:@"You pressed button %@", sender.tag]  
                                                    message: @"message" 
                                                   delegate: self 
                                          cancelButtonTitle: @"Ok" 
                                          otherButtonTitles: nil]];
    [alert show];
}

它看起来像这样:

enter image description here

您还可以稍微自定义警报视图,因此它可能具有文本输入的文本字段:

enter image description here

使用UIAlertViews,您还可以实现– alertView:clickedButtonAtIndex:– alertView:willDismissWithButtonIndex:等协议方法,以根据他们按下的警告按钮执行不同的操作。

这是一个关于UIAlertViews并实现其协议方法的好教程:http://mobile.tutsplus.com/tutorials/iphone/uialertview/

如果您不想使用UIAlertViews而是想要更加可自定义的模态视图,请查看这两个名为UAModalPanelMJPopupViewController的优秀库。您可以查看两个库中图像,演示和更多信息的链接,包括可以下载它们的github页面的链接。

希望这有帮助!

答案 2 :(得分:1)

确保警报显示在主线程上非常重要,主线程处理所有UI。否则你可能会遇到一些奇怪的错误和/或崩溃。您可以使用GCD调度到主线程:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert Test" 
                                          message:@"This is an alert test." 
                                          delegate:self 
                                          cancelButtonTitle:@"Cancel" 
                                          otherButtonTitles:@"OK", 
                                          nil];

dispatch_async(dispatch_get_main_queue(), ^{ 
    [alert show];
});
相关问题