2013-04-29 77 views
-1

我有這樣的代碼:的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

回答

0

下面的鏈接顯示,我們只能創建,並在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

1

嗯,得到當前應用對象

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