UILocalNotification没有开火

时间:2011-08-08 21:42:16

标签: iphone cocoa-touch ios4 uilocalnotification

非常对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;

3 个答案:

答案 0 :(得分:6)

如果您希望它在没有时间表的情况下显示(例如按下按钮时),请使用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;
    }
}

答案 1 :(得分:3)

你有没有安排闹钟?这是我用来触发警报的代码。

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

答案 2 :(得分:1)

如果您没有为UILocalNotification提供fireDate并将其与您的应用程序实例一起安排,则不会触发UIAlertView。如果您尝试立即提供某种警报,也许您应该尝试使用{{1}}。