当前位置: 动力学知识库 > 问答 > 编程问答 >

timer - iOS Local Notification Error

问题描述:

I have this code:

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

This works fine in the AppDelegate.m or (-(void) applicationDidEnterBackground:­(UIApplication *)application) but it does not work in the ViewController. I wanted this exact thing to happen when the user clicks a button so I need this code in ViewController not AppDelegate. How can I change this code so it can do the same thing in the ViewController! Thanks so much!

网友答案:

Following link shows that we can only create and schedule Local Notification in a AppDelegate file:

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

in the following methods: UIApplication scheduleLocalNotification: or presentLocalNotificationNow:

But the catch here is you can always get the instance of the AppDelegate and call these methods: MyApplicationDelegate *appDelegate = (MyApplicationDelegate *)[[UIApplication sharedApplication] delegate];

then call - [appDelegate presentLocalNotificationNow:LOcalnotificaton]

网友答案:

Um, get the current application object

[[UIApplication sharedApplication] setSchedualLocalNotifications:[NSArray arrayWithObject:notification]];
分享给朋友:
您可能感兴趣的文章:
随机阅读: