的iOS本地通知错误

问题描述:

我有这样的代码:的iOS本地通知错误

UILocalNotification *notification = [[UILocalNotification alloc] init]; 
[notification setAlertBody:@"This is a message"]; 
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:10]]; 
[notification setTimeZone:[NSTimeZone defaultTimeZone]]; 
[application setSchedualLocalNotifications:[NSArray arrayWithObject:notification]]; 

这正常的AppDelegate.m(-(void) applicationDidEnterBackground:­(UIApplication *)application)但它不会在ViewController工作。当用户点击一个按钮时,我想要发生这种确切的事情,所以我需要ViewController而不是AppDelegate这个代码。我如何更改此代码,以便它可以在ViewController中执行相同的操作!非常感谢!

+0

这个代码将在工作viewControllers还有些别的地方你去错了,我这么认为。 – Balu 2013-04-29 04:33:05

下面的链接显示,我们只能创建,并在AppDelegate的文件安排本地通知:

http://developer.apple.com/library/ios/#documentation/iPhone/Reference/UILocalNotification_Class/Reference/Reference.html

在下面的方法

: UIApplication scheduleLocalNotification: 或presentLocalNotificationNow:

但是这里的问题是你总是可以获得AppDelegate的实例并调用这些方法: MyApplicationDelegate * appDelegate =(MyApplicationDelegate *)[[UIApplication sharedApplication] delegate];

然后调用 - [的appDelegate presentLocalNotificationNow:LOcalnotificaton]

+0

你不需要一个委托,你需要UIApplication实例本身,你的代码将无法工作。 – 2013-05-02 19:49:20

+0

oops提出这个错误,请忽略意图是利用UIApplication:[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification]; – 2013-05-03 02:27:49

嗯,得到当前应用对象

[[UIApplication sharedApplication] setSchedualLocalNotifications:[NSArray arrayWithObject:notification]];