George Friday
George Friday

Reputation: 585

UIAlertView Textfield to Change UILabel Value

My app has a UILabel. I would like the user to be able to change the value of the label by pushing an "edit" button. I am able to implement a UIAlertView textfield with alert.alertViewStyle = UIAlertViewStylePlainTextInput, but I am not sure how the UILabel will receive the new value that was entered by the user.

This is what I have so far:

- (IBAction)edit
{


    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Edit New Amount"
                                                    message:@"Enter new rate"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Ok", nil];

    alert.alertViewStyle = UIAlertViewStylePlainTextInput;

    [alert show];

    UITextField *textField = [alert textFieldAtIndex:0];
    textField.placeholder = @"Enter New Rate";

}

I also implemented the UIAlertViewDelegate protocol.

Upvotes: 2

Views: 1507

Answers (1)

jszumski
jszumski

Reputation: 7416

Assuming you want to change the label when the user presses "Ok" and have a reference to UILabel *someLabel as an ivar:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    if (buttonIndex != alertView.cancelButtonIndex) {
        // UIAlertViewStylePlainTextInput will only ever have a single field at index 0
        UITextField *field = [alertView textFieldAtIndex:0];

        someLabel.text = field.text;

    } else {
        // this is where you would handle any actions for "Cancel"
    }
}

Upvotes: 3

Related Questions