简体   繁体   English

在目标C中创建默认值

[英]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. 我制作了一个计时器,只需按一下按钮即可计数,再按一次,便会转换为具有60秒倒计时的辅助计时器。

The problem lies in the fact that once I repeat this process, the (kRest-Pause) call is remembered. 问题在于,一旦我重复此过程,就会记住(kRest-Pause)调用。 I'm looking to create a default countdown of 60 without the continuation of the time. 我正在尝试创建默认的倒计时60,而不需要继续计时。 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. 我不知道该如何处理,我已经完成了-if语句返回的尝试,因为据我所知这不是这样。 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. 到目前为止,主计时器从10-0开始倒数​​,然后从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. 在这2分钟内,如果再次按下按钮,则0-120计数将暂停60秒,或者直到第三次按下按钮为止。 If this 60-0 countdown reaches 0 or is interrupted, the initial 0-120 countup resumes its count. 如果此60-0倒数达到0或被中断,则初始0-120倒数将恢复其计数。 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". 我的问题是,如果我第四次按下按钮,则从中断的那一刻起恢复60-0倒计时,而不会保留默认值60。这就是为什么我将帖子命名为“在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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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