2011-08-08 133 views
3

我是非常 Cocoa Touch和Objective-C的新手,但我已經獲得了相當大的一點,我正在嘗試使用UIKit。我有一個鏈接到一個按鈕,更改標籤,並觸發一個UILocalNotification,這是操作方法的動作:UILocalNotification沒有觸發

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

有任何錯誤或警告,但它只是不點火。我的行動方法有什麼問題嗎?

UPDATE
這裏是應用程序初始化代表包含UILocalNotification

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

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

請發表你的代碼,你居然安排或頁頭的'UILocalNotification'的一部分。 – fscheidl

+0

什麼是IBOulet?只有當你想將它與某些東西聯繫起來時纔是如此。同時使屬性和聲明具有相同的名稱(_NotifyOne或兩者均爲NotifyOne)。 –

+0

查看我更新的答案。 –

回答

6

如果您想讓它不顯示時間表(例如,當您按下按鈕時),請使用UIAlertView或將[application presentLocalNotificationNow:self.NotifyOne];添加到您的代碼中。

UPDATE

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

你應該把這個放在哪裏?應用程序委託的標題或實現? – nkcmr

+0

無論您想要通知何時觸發,它都會在您的實施中進行。它看起來像把它放在你的IBAction是你想要做的。 – jaminguy

1

一個UILocalNotification不會解僱,如果你不帶fireDate提供它與您的應用實例調度。如果您試圖立即提出某種警示,則可以嘗試使用UIAlertView

+1

Apple文檔說如果'fireDate'通過'nil',它會立即執行。 – nkcmr

+0

很高興知道,但如果不計劃開火,它不會做任何事情。 –