调用NSTimer方法

Str*_*rer 5 iphone objective-c nstimer ios

我刚开始与计时器合作,iPhone需要一些支持.

我有一个方法如下所示,花费时间并更新UILabel我的userinterface.我也有一个NSTimer调用该方法一次一次(我只显示小时和分钟).这个工作得很好,除了应用程序的第一秒实时(据我所知,在timer调用方法之前需要一秒钟).

我想调用我的方法viewDidLoad,但我怎样才能提供正确的参数?或者有更好的方法吗?

// Timer for updating time
[NSTimer scheduledTimerWithTimeInterval:1.0f 
                                 target:self 
                               selector:@selector(updateTime:) 
                               userInfo:nil 
                                repeats:YES];   


-(void)updateTime: (NSTimer *) timer {

    currentTime = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    NSString *currentTimeString = [dateFormatter stringFromDate:currentTime];
    [dateFormatter release];

    timeLabel.text =  currentTimeString;
}
Run Code Online (Sandbox Code Playgroud)

欣赏任何可以指向正确方向的东西.

vod*_*ang 2

在你的方法中,你没有使用计时器变量,对吧?那么,为什么只打电话呢[self updateTime:nil]?那应该有效