Brittany
Brittany

Reputation: 1439

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. E.g. Anything executed by tapping the button sendMessage should completely stop. 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

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



    }

Upvotes: 0

Views: 90

Answers (2)

Vincent
Vincent

Reputation: 4409

Add an if statement:

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

Upvotes: 0

Bista
Bista

Reputation: 7893

if (hour > 96) you do not want DIOSNode Block to be executed.

Then, do this, add a return:

else if (hour > 96) {

    points = @"5000";

    self.sendMessage.enabled = NO;

    return;
}

Upvotes: 2

Related Questions