【问题标题】:UILocalNotification not firingUILocalNotification 没有触发
【发布时间】:2011-08-08 21:42:15
【问题描述】:

我是 Cocoa Touch 和 Objective-C 的非常新手,但我已经掌握了很多要点,我正在尝试使用 UIKit。我有一个链接到更改标签并触发 UILocalNotification 的按钮的操作,这是操作方法:

- (IBAction)changeLabel:(id)sender {
    self.LabelOne.text = @"WHAT UPPP!";
    self.NotifyOne.alertBody = @"Testtttttt";
    self.NotifyOne.alertAction = @"Got It.";
    self.NotifyOne.fireDate = nil;
}

没有错误或警告,但它只是没有触发。我的操作方法有什么问题吗?

更新
这是包含 UILocalNotification 的 App Delegate 初始化:

@interface LearnAppDelegate : NSObject <UIApplicationDelegate> {
    UIButton *_ModifyLabelButton;
    UILabel *_LabelOne;
    UILocalNotification *_NotifyOne;
}

@property (nonatomic, retain) IBOutlet UILocalNotification *NotifyOne;

【问题讨论】:

  • 请将您的代码部分发布到您实际安排或分配UILocalNotification的位置。
  • IBoulet 有什么用?仅当您想将其与某些东西联系起来时。同时使属性和声明同名(_NotifyOne 或 NotifyOne)。
  • 无论如何,这一切似乎都被 iOS 5.0 打破了。或多或少...

标签: iphone cocoa-touch ios4 uilocalnotification


【解决方案1】:

如果您希望它在没有时间表的情况下显示(例如,当您按下按钮时),请使用 UIAlertView 或将 [application presentLocalNotificationNow:self.NotifyOne]; 添加到您的代码中。

更新

删除 IBOutlet 并使 UILocalNotification 的两个声明同名。例如:

@interface LearnAppDelegate : NSObject <UIApplicationDelegate> {
    UIButton *_ModifyLabelButton;
    UILabel *_LabelOne;
    UILocalNotification *NotifyOne;
}

@property (nonatomic, retain) UILocalNotification *NotifyOne;

记住在你的实现 (.m) 文件中synthesize

也可以试试这个:

- (IBAction)changeLabel:(id)sender {
    self.LabelOne.text = @"WHAT UPPP!";
    NotifyOne = [[UILocalNotification alloc] init];
    if (NotifyOne) {
        NotifyOne.alertBody = @"Testtttttt";
        NotifyOne.alertAction = NSLocalizedString(@"Got It.", nil);
        NotifyOne.fireDate = nil;
        NotifyOne.soundName = nil;
        NotifyOne.applicationIconBadgeNumber = 0;
        [application presentLocalNotificationNow:NotifyOne];
        [NotifyOne release];
        NotifyOne = nil;
    }
}

【讨论】:

    【解决方案2】:

    您是否安排过闹钟?这是我用来发出警报的代码。

    UILocalNotification *alarm = [[UILocalNotification alloc] init];
        if (alarm) {
            alarm.fireDate = [NSDate date];
            alarm.timeZone = [NSTimeZone defaultTimeZone];
            alarm.repeatInterval = 0;
            alarm.soundName = @"alarmsound.caf";
            alarm.alertBody = @"Test message...";       
            [[UIApplication sharedApplication] scheduleLocalNotification:alarm];
            [alarm release];
        }
    

    【讨论】:

    • 你应该把这个放在哪里? App Delegate 的标头还是实现?
    • 它将在您的实现中,无论您希望触发通知的任何位置。看起来将它放入您的 IBAction 是您想要做的。
    【解决方案3】:

    如果您不提供fireDate 并使用您的应用程序实例安排它,UILocalNotification 将不会触发。如果您想立即显示某种警报,也许您应该尝试使用UIAlertView

    【讨论】:

    • Apple 文档说如果 fireDate 被传递 nil 它将立即执行。
    • 很高兴知道,但如果它没有按计划启动,它就不会做任何事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    相关资源
    最近更新 更多