我的应用有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
协议。
答案 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"
}
}