objective c - iOS NSTimer not calling selector - not firing -
i have:
- (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically 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]]; }); }
but nothing called.
you're calling wrong selector. "setcurrenttime
" implementation doesn't take parameter (e.g. messaged or called, should use "selector:@selector(setcurrenttime)
".
now, if @ apple's documentation [nstimer scheduledtimerwittimeinterval: target: selector: userinfo: repeats:]
, apple says method should have signature:
- (void)setcurrenttime: (nstimer *) timer
which means function this:
-(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]]; }); }
and called this:
nstimer* timer = [nstimer scheduledtimerwithtimeinterval:0.25 target:self selector:@selector(setcurrenttime:) userinfo:nil repeats:yes];
Comments
Post a Comment