【发布时间】:2012-01-31 13:06:36
【问题描述】:
我有一个视图控制器,其中包含一个供用户输入日期值的字段。相比之下,我有一个名为“提醒前几天”的字段供用户选择何时触发通知。如果前一天的提醒是同一天,然后通知设置为在该日期触发,但是当提醒日在 1 天之前,则通知应在日期设置(指定)的一天之前触发。为此,我编写了一个名为 -(void)setNotification 的方法,在这里是实现代码:
- (void)setNotification
{
//Set notification after confirmation of saved data
Class cls = NSClassFromString(@"UILocalNotification");
UILocalNotification *notif = [[cls alloc] init];
if (cls != nil)
{
textField = [self.fields objectAtIndex:3];
if (textField.text == @"Same Day")
{
notif.fireDate = [datePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];
}
else if(textField.text == @"1 Day")
{
NSDate *now = [datePicker date];
// set up date components
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:now];
[components setDay:-1];
// create a calendar to form date
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:now options:0];
notif.fireDate = newDate2;
notif.timeZone = [NSTimeZone defaultTimeZone];
}
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = textView.text;
notif.alertAction = @"View";
notif.soundName = @"lazy_afternoon.mp3";
notif.applicationIconBadgeNumber = 1;
textField = [self.fields objectAtIndex:1];
NSDictionary *userDict = [NSDictionary dictionaryWithObject:self.textField.text forKey:kReminder];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
}
现在我们都知道,当通知被触发时,用户点击视图。然后我们显示一个警报,实现代码写在 appDelegate 中。这里是:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// For The Purpose Of Notification.
Class cls = NSClassFromString(@"UILocalNotification");
if (cls)
{
UILocalNotification *notification = [launchOptions objectForKey:
UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
NSString *reminderText = [notification.userInfo objectForKey:kReminder];
[self.viewController showReminder:reminderText];
}
}
application.applicationIconBadgeNumber = 0;
}
现在收到本地通知后,我们执行以下操作,即:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
application.applicationIconBadgeNumber = 0;
NSString *reminderText = [notification.userInfo objectForKey:kReminder];
[self.viewController showReminder:reminderText];
}
现在我已将 -(void)setNotification 操作设置为标题为“保存”的右侧导航栏按钮项,如下所示:
-(IBAction)save:(id)sender
{
[self setNotification];
}
当我没有为开火日期指定任何条件时,即简单地分配为:
notif.fireDate = [datePicker 日期];通知一切正常(没有问题)。
但是当我按照上述操作时,即触发日期的条件,则不会触发通知。相反,当我单击保存时会触发警报。此外,当我退出模拟器时,我可能会看到一些线程问题。我不明白代码(实现)有什么问题。我浏览了几个链接,也是 UILocalNotification 的苹果documentation。找不到任何属性或方法来根据条件设置触发日期。
我发现了一种方法“repeatTimeInterval”,当通知必须每周、每年等重复一次时,该方法是相关和适用的textField 中的天数是这样的”
谁能指导我一下,
提前谢谢大家:)
【问题讨论】:
-
有人觉得这个帖子有点长吗??
-
@Sarah 对不起,我想具体一点,以便专家可以更好地理解问题并轻松回答。您能帮帮我吗,谢谢 :)
-
您能否简要解释一下,因为由于时间较短,我无法阅读全文。
-
哦,当然,我有一个文本字段可以在几天前选择提醒。如果提醒日是同一天,那么通知触发日期将是 [datePicker date];当它是 1 天时,应该触发通知在从 datePicker 选择的 1 天之前。但是通知没有触发,而是显示警报,当我们单击通知的查看按钮时应该显示该警报,当我退出模拟器时,出现线程问题,这就是问题,谢谢 :)
-
我需要赶时间。仍然会告诉您在代码中放置断点并尝试调试,您肯定会遇到错误。对不起..
标签: iphone uilocalnotification