ios scheduledTimerWithTimeInterval for amount of time

耗尽温柔 提交于 2019-12-23 09:07:14

问题


I want to use scheduledTimerWithTimeInterval to do a periodic task for certain amount of time lets say one hour. but How do I implement on my code. Here is my code:

timer = [NSTimer scheduledTimerWithTimeInterval: 0.1
                                          target: self
                                        selector: @selector(doSomething:)
                                        userInfo: nil
                                         repeats: YES];

回答1:


Lets assume for a minute .

Here is simple scenario ,

int remainingCounts;
NSTimer *timer;

timer = [NSTimer scheduledTimerWithTimeInterval:1 target:(self) selector:@selector(countDown) userInfo:nil repeats:YES];
remainingCounts = 60;   // int remainingCounts ;...

Method being called for every sec.

-(void)countDown {
    NSLog(@"%d", remainingCounts);
    // Do Your stuff......
    if (--remainingCounts == 0) {
        //[self dismissViewControllerAnimated:YES completion:nil]; 
        [timer invalidate];
    }
}


来源:https://stackoverflow.com/questions/17225164/ios-scheduledtimerwithtimeinterval-for-amount-of-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!