简体   繁体   English

iOS-取消UIButton操作

[英]iOS - Cancel UIButton action if

I've created a button that executes a series of items (not all shown in this code snippet to keep it brief). 我创建了一个执行一系列项目的按钮(为了简短起见,该代码段中并未全部显示)。 If my NSString (points) is greater than 96, I want the button action to cancel. 如果我的NSString(点)大于96,我希望取消按钮操作。 Eg Anything executed by tapping the button sendMessage should completely stop. 例如,通过点击sendMessage按钮执行的所有操作均应完全停止。 I'm using the below code now, and it doesn't seem to stop the action from completing. 我现在正在使用以下代码,它似乎并没有阻止操作完成。 Any idea why this might be? 知道为什么会这样吗?

ViewController.h ViewController.h

- (IBAction)sendMessage:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *sendMessage;

ViewController.m ViewController.m

     - (IBAction)sendMessage:(id)sender {

            NSString *points;

            if (hour <= 1) {

                points = @"300";


            } else if (hour > 1 && hour < 9) {

                points = @"600";


            } else if (hour <= 9) {

                points = @"1000";

            } else if (hour < 9 && hour < 24) {

                points = @"1000";


            } else if (hour <= 48) {

                points = @"2000";

            } else if (hour > 48 && hour <= 72) {

                points = @"3000";

            } else if (hour > 72 && hour <= 96) {

                points = @"4000";

            } else if (hour > 96) {

                points = @"5000";

               self.sendMessage.enabled = NO;

              self.exceedsFive.hidden = NO;

            }

 [DIOSNode nodeSave:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {


            [self dismissViewControllerAnimated:YES completion:nil];


        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Node did not save!");
        }];



    }

if (hour > 96) you do not want DIOSNode Block to be executed. if (hour > 96) ,则不希望执行DIOSNode Block。

Then, do this, add a return : 然后,执行此操作,添加一个return

else if (hour > 96) {

    points = @"5000";

    self.sendMessage.enabled = NO;

    return;
}

Add an if statement: 添加一条if语句:

If (points < 500) {
 [DIOSNode nodeSave:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {


            [self dismissViewControllerAnimated:YES completion:nil];


        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Node did not save!");
        }];

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

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