iOS - 如果要取消UIButton操作

时间:2016-12-03 05:17:01

标签: ios objective-c uibutton

我创建了一个执行一系列项目的按钮(并非所有显示在此代码段中以保持简短)。如果我的NSString(点数)大于96,我希望取消按钮操作。 例如点击sendMessage按钮执行的任何操作都应该完全停止。我现在使用下面的代码,似乎并没有阻止操作完成。知道为什么会这样吗?

ViewController.h

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

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!");
        }];



    }

2 个答案:

答案 0 :(得分:2)

if (hour > 96)您不希望执行DIOSNode Block。

然后,执行此操作,添加return

else if (hour > 96) {

    points = @"5000";

    self.sendMessage.enabled = NO;

    return;
}

答案 1 :(得分:0)

添加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!");
        }];