UILocalNotification 未触发

发布于 2024-11-29 01:57:56 字数 728 浏览 0 评论 0原文

我对 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 的应用程序委托初始化:

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

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

I am very new to Cocoa Touch and Objective-C but I've gotten the pretty big points down and I am experimenting with the UIKit. I have a action linked to a button that changes a label and fires a UILocalNotification and this is the action method:

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

There are no errors or warnings, but it's just not firing. Is there anything wrong with my action method?

UPDATE
Here is the App Delegate initialization that contains the UILocalNotification:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黯然 2024-12-06 01:57:56

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

If you are wanting it show without a schedule (eg. when you press a button) then either use UIAlertView or add [application presentLocalNotificationNow:self.NotifyOne]; to your code.

UPDATE

Remove IBOutlet and make both declarations of UILocalNotification the same name. For example:

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

@property (nonatomic, retain) UILocalNotification *NotifyOne;

Remember to synthesize in your implementation (.m) file.

Also try this instead:

- (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;
    }
}
一腔孤↑勇 2024-12-06 01:57:56

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

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

Are you ever scheduling the alarm? Here is code I use to fire alarms.

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];
    }
樱娆 2024-12-06 01:57:56

如果您不向 UILocalNotification 提供 fireDate 并将其安排到您的应用程序实例,则 UILocalNotification 不会触发。如果您尝试立即呈现某种警报,也许您应该尝试使用 UIAlertView

A UILocalNotification isn't going to fire if you don't provide it with a fireDate and schedule it with your application instance. If you're trying to immediately present some sort of alert, perhaps you should try using UIAlertView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文