UIAlertView Textfield更改UILabel值

时间:2013-04-14 04:28:11

标签: ios objective-c uialertview

我的应用有UILabel。我希望用户能够通过按“编辑”按钮来更改标签的值。我可以使用UIAlertView实现alert.alertViewStyle = UIAlertViewStylePlainTextInput文本字段,但我不确定UILabel将如何接收用户输入的新值。

这是我到目前为止所做的:

- (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";

}

我还实施了UIAlertViewDelegate协议。

1 个答案:

答案 0 :(得分:3)

假设您想在用户按下“确定”时更改标签,并将UILabel *someLabel引用为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"
    }
}