3

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;
nkcmr
  • 10,690
  • 25
  • 63
  • 84
  • Please post the part of your code where you actually schedule or alloc the `UILocalNotification`. – fscheidl Aug 08 '11 at 21:56
  • What's the IBOulet for? That is only if you want to connect it up with something. Also make both the property and the declaration the same name (both _NotifyOne or both NotifyOne). – Bo A Aug 08 '11 at 22:06
  • Anyway this all seems broken with iOS 5.0. More or less... – Jonny Oct 18 '11 at 03:01

3 Answers3

6

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;
    }
}
Bo A
  • 3,144
  • 2
  • 33
  • 49
3

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];
    }
jaminguy
  • 25,840
  • 2
  • 23
  • 21
  • Where are you supposed to put this? Header or Implementation of the App Delegate? – nkcmr Aug 08 '11 at 21:51
  • It would be in your implementation wherever you want the notification to fire. It looks like putting it in your IBAction is what you want to do. – jaminguy Aug 08 '11 at 22:07
1

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.

Mark Adams
  • 30,776
  • 11
  • 77
  • 77