试图使用NSTimer

时间:2011-10-06 05:13:43

标签: ios ios4 nstimer

我在使NSTimer工作时遇到问题,可能是因为我不知道如何正确使用它(我试过阅读苹果文档,它对我帮助不大)。我得到UIDatePicker的时间,我希望应用程序在达到该时间时调用该方法。简单,不是吗?所以我的代码如下:

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

现在有一些事情遵循标准,但它仍然无效。它仍然给我6小时的时间,当它到达那个时间时,它什么也没做。

我的alarmTimer类的RunTimer方法的代码如下:

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

现在有一些事情遵循标准,但它仍然无效。它仍然给我6小时的时间,当它到达那个时间时,它什么也没做。

如果有帮助,这里是视图控制器的.h和.m文件: .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

AlarmTimer类的代码:

.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

最后,.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

非常感谢能够提供帮助的任何人!

2 个答案:

答案 0 :(得分:1)

为什么使用NSDateFormatter?试着这样做:

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

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

[alarmTimer RunTimer: self.Picker.date];

答案 1 :(得分:0)

  1. 通常,同时执行+ alloc和-init:Foo *someFoo = [[Foo alloc] init];。不要像使用timeralarmTimer变量那样将调用分开。这样做的原因是,有时初始化程序将返回与+alloc得到的指针不同的指针,当发生这种情况时,您希望确定存储该新指针。我不知道NSTimer是否会这样做,但如果是这样的话,你现在的代码会遇到各种麻烦。

  2. 不要尝试重复使用计时器。一旦它发射,释放它并在需要时创建一个新的。

  3. -SetButtonPress:中的那种疯狂是什么?您从日期选择器获取日期,将其转换为字符串,然后立即将该字符串转换回日期...为什么?

  4. 您的计时器间隔为24小时(86400秒)。您是否错误地认为具有较长间隔的计时器会唤醒后台应用程序? NSTimer依赖于运行循环;当运行循环未运行时,计时器不工作。

  5. 如果您坚持使用通常的Objective-C约定并以小写字符开头的名称方法,即-runTimer:而不是-RunTimer:,那么帮助您会更容易一些。同样的规则适用于实例变量。