Ios 正在尝试使用NSTimer

Ios 正在尝试使用NSTimer,ios,ios4,nstimer,Ios,Ios4,Nstimer,我在使用NSTimer时遇到了问题,可能是因为我不知道如何正确使用它(我试过阅读苹果的文档,它对我没有多大帮助)。我从UIDatePicker获得一段时间,我希望应用程序在达到该时间时调用该方法。简单,不是吗?我的代码如下: -(IBAction)SetButtonPress:(id)sender{ NSDate *date = [Picker date]; alarmTimer = [[AlarmTimer alloc] init]; [alarmTimer run

我在使用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:

以及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

非常感谢任何能帮忙的人

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

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

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

[alarmTimer RunTimer: self.Picker.date];
  • 通常,同时执行+alloc和-init:
    Foo*someFoo=[[Foo-alloc]init]。不要像使用
    timer
    alarmTimer
    变量那样分离调用。原因是,有时初始值设定项返回的指针与您从
    +alloc
    中获得的指针不同,当这种情况发生时,您需要确保存储新指针。我不知道NSTimer是否曾经这样做过,但如果是这样的话,您将在当前代码中遇到各种各样的问题

  • 不要试图重复使用计时器。一旦它启动,释放它并在需要时创建一个新的

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

  • 您使用的计时器间隔为24小时(86400秒)。你是否误以为一个长时间间隔的计时器会唤醒后台的应用程序?NSTimer依赖于运行循环;当运行循环未运行时,计时器不工作

  • 如果您坚持使用通常的Objective-C约定和以小写字符开头的命名方法,即
    -runTimer:
    而不是
    -runTimer:
    ,则会更容易为您提供帮助。同样的规则也适用于实例变量


  • 我刚才试过了。当我将时间设置为12:09时,它在NSLog中显示为6:09。而且,我不认为NSTimer本身工作不正常(如果我之前将时间设置为6小时,它仍然不会运行该方法)好的,尝试将NSDateFormatter与ndf一起使用。timeZone=[NSTimeZone localTimeZone];和setDateFormat:ndf的格式为self.PickerDate.date。然后获取stringFromDate:Picker.Date。和[alarmTimer RunTimer:[ndf dateFromString:time]];我已经改变了init/alloc的内容,并重命名了一些变量。它仍然不起作用。我将其设置为今天早上2:32(10月6日),NSLog中显示NSDate是9月28日的8:32。那么,您所在的时区是什么?Mountain(美国和加拿大)GMT-0700NSDate始终使用GMT,因此,如果您直接记录日期,您将获得GMT时间。请使用正确配置的日期格式化程序以本地时间打印时间。但是,没有必要将日期转换为字符串,然后再将其转换回日期,这只是浪费时间。您只需要在希望显示日期时使用格式化程序。出于日志记录的目的,可能更容易记录日期对象本身并在头脑中从GMT转换。请注意,我们仍在观察夏令时,MDT是UTC-6,因此2:32 GMT对应于8:32 MDT。我不知道为什么你得到的是9月28日而不是10月8日;它可以是您对日历的选择,也可以是您配置日期选择器的其他方式。
    .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
    
    //
    //  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
    
    alarmTimer = [AlarmTimer alloc];
    [alarmTimer init];
    
    NSLog(@"Date is: %@", self.Picker.date);
    
    [alarmTimer RunTimer: self.Picker.date];