NSTimer problem

后端 未结 2 491
野的像风
野的像风 2020-12-03 20:32

So I am trying to set up a basic timer but I am failing miserably. Basically all I want is to start a 60 second timer when the user clicks a button, and to update a label wi

2条回答
  •  忘掉有多难
    2020-12-03 20:55

    It may be a little late to post a second answer to this question but I've been looking for a good place to post my own solution to this problem. In case it is of use to anyone here it is. It fires 8 times but of course this can be customised as you please. The timer deallocates itself when time is up.

    I like this approach because it keeps the counter integrated with the timer.

    To create an instance call something like:

    SpecialKTimer *timer = [[SpecialKTimer alloc] initWithTimeInterval:0.1 
                                                             andTarget:myObject
                                                           andSelector:@selector(methodInMyObjectForTimer)];
    

    Anyway, here are the header and method files.

    //Header
    
    #import 
    
    @interface SpecialKTimer : NSObject {
    
        @private
    
        NSTimer *timer;
    
        id target;
        SEL selector;
    
        unsigned int counter;
    
    }
    
    - (id)initWithTimeInterval:(NSTimeInterval)seconds
                     andTarget:(id)t
                   andSelector:(SEL)s;
    - (void)dealloc;
    
    @end
    
    //Implementation
    
    #import "SpecialKTimer.h"
    
    @interface SpecialKTimer()
    
    - (void)resetCounter;
    - (void)incrementCounter;
    - (void)targetMethod;
    
    @end
    
    @implementation SpecialKTimer
    
    - (id)initWithTimeInterval:(NSTimeInterval)seconds
                     andTarget:(id)t
                   andSelector:(SEL)s {
    
        if ( self == [super init] ) {
    
            [self resetCounter];
    
            target = t;
            selector = s;
    
            timer = [NSTimer scheduledTimerWithTimeInterval:seconds
                                                     target:self
                                                   selector:@selector(targetMethod)
                                                   userInfo:nil
                                                    repeats:YES];
    
        }
    
        return self;
    
    }
    
    - (void)resetCounter {
    
        counter = 0;
    
    }
    
    - (void)incrementCounter {
    
        counter++;
    
    }
    
    - (void)targetMethod {
    
        if ( counter < 8 ) {
    
            IMP methodPointer = [target methodForSelector:selector];
            methodPointer(target, selector);
    
            [self incrementCounter];
    
        }
    
        else {
    
            [timer invalidate];
            [self release];
    
        }
    
    }
    
    - (void)dealloc {
    
        [super dealloc];
    
    }
    
    @end
    

提交回复
热议问题