我对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;
最佳答案
如果您希望它没有时间表显示(例如,当您按下按钮时),请使用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;
}
}