[objc] NSTimer 사용법

천현철·2021년 8월 17일
0

iOS

목록 보기
45/51
post-custom-banner

타이머 시작/멈춤:

// Private Methods
@interface MyClass ()
{
    NSTimer *_timer;
}
- (void)_timerFired:(NSTimer *)timer;
@end

@implementation MyClass

- (IBAction)startTimer:(id)sender {
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                  target:self
                                                selector:@selector(_timerFired:)
                                                userInfo:nil
                                                 repeats:YES];
    }
}

- (IBAction)stopTimer:(id)sender {
    if ([_timer isValid]) {
        [_timer invalidate];
    }
    _timer = nil;
}

- (void)_timerFired:(NSTimer *)timer {
    NSLog(@"ping");
}

https://stackoverflow.com/questions/12052914/how-can-i-start-and-stop-nstimer/12053057

profile
기도하지말고 행동하라
post-custom-banner

0개의 댓글