简体   繁体   中英

Start/stop button not working correctly iOS

I got into a very noob problem which I can not seem to solve in any way, I am sure I am overlooking something, but cannot figure it out for gods sake!!

Okay, so I have a start/stop button in my app, it should resume the action if the action is paused and pause the action if it is already running. Here is my code:

- (IBAction)pause:(id)sender {

    if(paused){
        [self.progressBar resumeLayer];
        paused = false;
        NSLog(@"resume");
    }

    if(!paused){
        [self.progressBar pauseLayer];
        paused = true;
        NSLog(@"pause");

    }

}

The problem is: When i run the app and press on the button to pause, it works fine however after that it won't resume at all. After NSLogging to the console, I found that pause is called right after resume.... Why is this?? First: the button is clicked only once, how can it call two opposite methods? Second: Why isn't my BOOL check working?

EDIT: If I swap out one of the if statements to an else if, it works fine... Why is that?

Thank you!

请以Paused变量作为BOOL并使用YES和NO进行设置

What is "paused"? A global variable? An instance variable? I don't know.

Convention is that instance variables should start with an underscore character, which they will do automatically if you just define a property. That way people have at least some idea what is happening.

Convention for BOOL values is YES or NO. Not true or false.

The real bummer is of course a stupid bug in your code. If paused == YES then the first if is executed, paused is set to NO, and then in the next test you check that paused == NO - which it is at this point.

You would have found that easily by stepping through the code in the debugger, line by line.

- (IBAction)pause:(id)sender {

    if(paused){
        [self.progressBar resumeLayer];
        NSLog(@"resume");
    }

    if(!paused){
        [self.progressBar pauseLayer];
        NSLog(@"pause");
    }

    paused = !paused;

}

You should check for the other state in an else block instead of having two if statements.

The reason why this fails for you now is that, you change the state of paused to false and immediately after the first if statement is done you check for !paused . That will be true as you just changed it to false.

To fix it, you either can use an else if (!paused) statement or just a plain else if the paused variable is a boolean.

Something like this:

- (IBAction)pause:(id)sender {

    if(paused){
        [self.progressBar resumeLayer];
        paused = false;
        NSLog(@"resume");
    } else {
        [self.progressBar pauseLayer];
        paused = true;
        NSLog(@"pause");
    }
}
1) Declare first BOOL variable in @interface ViewController ()
Ex. bool start;
2) Initialize BOOL variable in viewDidLoad method Like this start = YES;
3) now write code in button Action Like this

- (IBAction)btnPause:(UIButton *)btn {
    if (start == YES)
    {
        btn.backgroundColor = [UIColor blackColor];
        start = NO;
    }
    else
    {
        btn.backgroundColor = [UIColor whiteColor];
        start = YES;
    }
}

// Its change button background color but you can use your Logic.

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