2

我的应用程序有一个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协议。

4

1 回答 1

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"
    }
}
于 2013-04-14T04:36:19.973 回答