简体   繁体   中英

Creating defaults in Objective C

I'm working on my first big application and have inadvertently driven myself into a panic from a small flaw in design. I've made a timer that counts at the touch of a button, and upon a second touch, transitions into a secondary timer with a 60 second countdown.

The problem lies in the fact that once I repeat this process, the (kRest-Pause) call is remembered. I'm looking to create a default countdown of 60 without the continuation of the time. Should I kill it in memory and create a new instance for each subsequent button press? Or is there a logic game that looks at the aggregate time and corrects with each new occurance?

I don't know how to approach this, I'm done attempting -if statements with returns as I have learned that's not how that works. Any help would be appreciated

EDIT: Sorry for the lack of clarity. I'm pretty green on programming of any caliber. So far, the main timer counts down from 10-0 then up from 0-120. In that 2 minute period, if the button is pressed again, the 0-120 count is paused for 60 seconds or until the button is pressed for the third time. If this 60-0 countdown reaches 0 or is interrupted, the initial 0-120 countup resumes its count. My issue is that if I push the button a fourth time, the 60-0 countdown is resumed from the moment of interruption without retaining a default of 60. This is why I named the post "creating defaults in Objective C". It's the wrong use of the word and way to broad, but it's what I could come up with.

kRest=60

-(void)increase{

    if (mode==1){
        count++;

        int d = 10-count;

        if (d==0){ timeLabel.text = @"Begin";
            [self startPlaybackForPlayer: self.startTimerSound];}

        else {timeLabel.text = [NSString stringWithFormat:@"%d", abs(d)];}

        if(d<0){
            //workign out
            active = TRUE;
            if (d <= -20) {
                [self stopTimer];                 
            }
        }
        else{
            //no user interface
            active = FALSE;



        }
    }
    else{
        pause++;
        countdownLabel.text = [NSString stringWithFormat:@"%d!", (kRest-pause)];
        NSLog(@"Paused at time %d", pause);


        UIColor *textColor = nil;
        if (pause % 2==0){
            textColor = [UIColor yellowColor];
        }
        else{
            textColor = [UIColor redColor];

        }
        timeLabel.textColor = textColor;

        if ((kRest-pause)==0){
            countdownLabel.text = [NSString stringWithFormat:@"%d!",pause];
            mode=1;
            pause=0;
            [button setTitle:@"Stop" forState:UIControlStateNormal];
            repCount++;
            myRepCount.text = [NSString stringWithFormat:@"Rep Count: %d", repCount];
            countdownLabel.text = @"";
        }
    }
}

if you are using a timer to access this counter then you should be able to update the counter that the timer uses. Just make sure you synchronize the object so you dont edit while you are reading.

here is an example of what I mean.

int counter = 0;

int limit = 60;

- (BOOL) incrementUntilReached{

    @synchronized(self){
        if (counter == limit) return YES;
        counter++;
        return NO;
    }
}

- (void) resetTimer{
    @synchronized(self){
        counter = 0;
    }
}

- (int) countsLeft {
    @synchronized(self){
        return limit - counter;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM