简体   繁体   English

开始/停止按钮无法正常运行iOS

[英]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?? NSLogging到控制台后,我发现恢复后立即调用了暂停。 First: the button is clicked only once, how can it call two opposite methods? 第一:仅单击一次按钮,如何调用两个相反的方法? Second: Why isn't my BOOL check working? 第二:为什么我的BOOL检查不起作用?

EDIT: If I swap out one of the if statements to an else if, it works fine... Why is that? 编辑:如果我将if语句之一换成else if,则可以正常工作...为什么呢?

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. BOOL值的约定为是或否。 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. 如果暂停==是,则执行第一个if,暂停设置为NO,然后在下一个测试中检查暂停==否-此时为。

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. 您应该在else块中检查其他状态,而不要使用两个if语句。

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 . 现在对您失败的原因是,您将暂停状态更改为false并且在完成第一个if语句后立即检查!paused That will be true as you just changed it to false. 只需将其更改为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. 要解决此问题,您可以使用else if (!paused)语句,或者如果已暂停的变量是布尔值, else if (!paused)可以使用纯else语句。

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.

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

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