Jack
Jack

Reputation: 950

Trying to use an NSTimer

I'm having a problem getting an NSTimer to work, probably because I don't know how to properly use it (I've tried reading the apple documentation, it didn't help me much). I get a time off of a UIDatePicker and I want the app to call the method when that time is reached. Simple, no? So the code I have is below:

-(IBAction)SetButtonPress:(id)sender{
     NSDate *date = [Picker date];
     alarmTimer = [[AlarmTimer alloc] init];
    [alarmTimer runTimer: Picker.date];
}

A few things now follow standards, but it still doesn't work. It still gives me a time 6 hours ahead, and when it hits that time, it doesn't do anything.

And the code for the RunTimer method of my alarmTimer class is as follows:

-(void)RunTimer: (NSDate *) date
{
NSRunLoop *theLoop = [NSRunLoop currentLoop];
[theLoop addTimer:timer forMode:NSDefaultRunLoopMode];
[timer initWithFireDate:date interval:0 target:self selector:@selector(Play) userInfo: nil repeats:NO];
}

A few things now follow standards, but it still doesn't work. It still gives me a time 6 hours ahead, and when it hits that time, it doesn't do anything.

And just in case it helps, here is the .h and .m files for the view controller: .h:

//
//  Assignment_1ViewController.h
//  Assignment 1
//
//  Created by Jack Schaible on 11-09-28.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "AlarmTimer.h"

@interface Assignment_1ViewController : UIViewController {
    UIButton *SetButton;
    UIDatePicker *Picker;
    UIButton *CancelButton;
    NSString *time;
    AlarmTimer *alarmTimer;
}

@property (nonatomic, retain) IBOutlet UIButton *SetButton;
@property (nonatomic, retain) IBOutlet UIDatePicker *Picker;
@property (nonatomic, retain) IBOutlet UIButton *CancelButton;

- (IBAction)SetButtonPress:(id)sender;
- (IBAction)CancelButtonPress:(id)sender;
@end

.m:

//
//  Assignment_1ViewController.m
//  Assignment 1
//
//  Created by Jack Schaible on 11-09-28.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Assignment_1ViewController.h"
#import "AlarmTimer.h"

@implementation Assignment_1ViewController
@synthesize Picker;
@synthesize CancelButton;
@synthesize SetButton;

- (void)dealloc
{
    [SetButton release];
    [Picker release];
    [CancelButton release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}


- (void)viewDidUnload
{
    [self setSetButton:nil];
    [self setPicker:nil];
    [self setCancelButton:nil];
    [super viewDidUnload];
    [alarmTimer release];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)SetButtonPress:(id)sender {
    NSDateFormatter *ndf = [[NSDateFormatter alloc] init];

    time = [ndf stringFromDate:self.Picker.date];

    alarmTimer = [AlarmTimer alloc];
    [alarmTimer init];

    NSLog(@"Date is: %@", [ndf dateFromString:time]);

    [alarmTimer RunTimer:[ndf dateFromString:time]];
}

- (IBAction)CancelButtonPress:(id)sender {
}
@end

And the code for the AlarmTimer class:

.h:
//
//  Timer.h
//  Assignment 1
//
//  Created by Jack Schaible on 11-09-28.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

@interface AlarmTimer : NSObject {
    NSString *time;
    NSTimer *timer;
    AVAudioPlayer *player;
}

-(void) RunTimer: (NSDate *)date;
-(void)Play;
-(void)CancelTimer;

@end

And finally, the .m:

//
//  Timer.m
//  Assignment 1
//
//  Created by Jack Schaible on 11-09-28.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "AlarmTimer.h"
@implementation AlarmTimer

-(id) init
{
    if(self == [super init])
    {
        timer = [NSTimer alloc];
        NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Time.mp3", [[NSBundle mainBundle] resourcePath]]];
        NSError *error;
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    }
    return self;
}

- (void) RunTimer: (NSDate *)date
{
    [timer initWithFireDate:date interval:86400 target:self selector:@selector(Play) userInfo:nil repeats:NO];
    NSLog(@"Date is: %@", date);
}

-(void) Play
{
    [player play];
    UIAlertView *aView = [[UIAlertView alloc] initWithTitle:@"Holy Chimes!" message:@"If you didn't hear that..." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
    [aView show];
}

-(void)CancelTimer
{
    time = nil;
}

@end

Thanks a lot to anyone who can help!

Upvotes: 0

Views: 311

Answers (2)

Caleb
Caleb

Reputation: 125017

  1. As a rule, do the +alloc and -init together: Foo *someFoo = [[Foo alloc] init];. Don't separate the calls as you've done with your timer and alarmTimer variables. The reason for this is that sometimes the initializer will return a different pointer than what you get from +alloc, and when that happens you want to be certain to store that new pointer. I don't know if NSTimer ever does that, but if so you'll have all kinds of trouble with your current code.

  2. Don't try to reuse a timer. Once it fires, release it and create a new one when you need to.

  3. What's all that craziness in -SetButtonPress:? You get a date from a date picker, convert it to a string, and then immediately convert that string back to a date... why?

  4. You're using an interval of 24 hours (86400 seconds) for your timer. Are you under the mistaken impression that a timer with a long interval will wake up an app that's in the background? NSTimer relies on the run loop; when the run loop isn't running, the timer isn't working.

  5. It'd be somewhat easier to help you if you'd stick to the usual Objective-C convention and name methods starting with lower-case characters, i.e. -runTimer: instead of -RunTimer:. The same rule applies to instance variables.

Upvotes: 0

blackhawk4152
blackhawk4152

Reputation: 389

Why do you use NSDateFormatter? try to do it like that:

alarmTimer = [AlarmTimer alloc];
[alarmTimer init];

NSLog(@"Date is: %@", self.Picker.date);

[alarmTimer RunTimer: self.Picker.date];

Upvotes: 1

Related Questions