1

我有:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setNeedsStatusBarAppearanceUpdate];
    NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(setCurrentTime:)  userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    [timer fire];

}
-(void)setCurrentTime{
    NSLog(@"TEST");
    dispatch_async(dispatch_get_main_queue(), ^{
        NSDate *currentDate = [[NSDate alloc] init];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm"];
        [currentTime setText:[dateFormatter stringFromDate:currentDate]];
    });
}

但是什么都没有被调用。

4

1 回答 1

7

你调用了错误的选择器。您的“ setCurrentTime”实现不采用任何参数(例如,要正确发送消息或调用,您应该使用“ selector:@selector(setCurrentTime)”。

现在,如果您查看Apple 的文档[NSTimer scheduledTimerWitTimeInterval: target: selector: userInfo: repeats:],Apple 说您的方法应该具有以下签名:

- (void)setCurrentTime: (NSTimer *) timer

这意味着您的函数将如下所示:

-(void)setCurrentTime: (NSTimer *) timer
{
    NSLog(@"TEST");
    dispatch_async(dispatch_get_main_queue(), ^{
        NSDate *currentDate = [[NSDate alloc] init];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm"];
        [currentTime setText:[dateFormatter stringFromDate:currentDate]];
    });
}

并被称为:

NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:0.25 
                   target:self 
                 selector:@selector(setCurrentTime:)  
                 userInfo:nil 
                  repeats:YES];
于 2013-10-07T05:45:46.703 回答