Search code examples
iosif-statementxcode4.5uialertviewibaction

Calling an IBaction in a alertView if statement


Me and my buddy are working on an app, we're total newbies but have come a long way with books and goggling.

We're stuck now on this thing. We have a bunch of texfields that we have clear button linked to it with this action, but then we want that action to be called if you click "Yes" on one of the alert view buttons.

- (IBAction)clearText:(id)sender {

Spelare1Slag1.text = @"";
Spelare1Slag2.text = @"";

}

We also have this alert view:

        alertDialog = [[UIAlertView alloc]
    initWithTitle: @"Warning"
    message: @"Do you want to delete?"
    delegate: self
    cancelButtonTitle: @"No"
    otherButtonTitles: @"Yes", nil];

- (void)alertView: (UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
    if ([buttonTitle isEqualToString:@"No"]) {
    }

    else if ([buttonTitle isEqualToString:@"Yes"]){
        Spelare1Slag1.text = @"";
    }

}

So this is how we think we should do it, but we don't know what to put in the else if statement. We want the textfields to clear when you press the "yes" button in the alert view, and not when you press "no"

Thanks in advance!


Solution

  • The clearText method, I'm assuming, is a custom method you created to delete the text in both the fields right? So instead of it being an IBAction, it should be a void method :

    - (void)clearText {
    Spelare1Slag1.text = @"";
    Spelare1Slag2.text = @"";
    }
    

    Now all you need to do in your UIAlertView delegate method, is call the clearText method :

    - (void)alertView: (UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
      if ([buttonTitle isEqualToString:@"Yes"]){
          [self clearText];
      }
    }
    

    Hope this helps