gpt4 book ai didi

ios - UIAlertView 中的 UITextField 无法设置光标

转载 作者:行者123 更新时间:2023-11-29 13:02:23 25 4
gpt4 key购买 nike

这是我的代码:

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Confirm Address" message:@"Please confirm your address:" delegate:self cancelButtonTitle:@"Confirm" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.text = @"1234 ABC Street";
[alertTextField becomeFirstResponder];
UITextPosition *beginning = [alertTextField beginningOfDocument];
[alertTextField setSelectedTextRange: [alertTextField
textRangeFromPosition:beginning
toPosition:beginning]];

[alert show];

光标未设置到文本字段的开头。我做错了什么?

最佳答案

实现UITextFieldDelegate在你的 Controller 上,移动你的 setSelectedTextRange textFieldDidBeginEditing 的代码委托(delegate)回调:

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Confirm Address" message:@"Please confirm your address:" delegate:self cancelButtonTitle:@"Confirm" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.text = @"1234 ABC Street";
[alertTextField becomeFirstResponder];

// Your UITextFieldDelegate implementer here
[alertTextField setDelegate:self];

[alert show];

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITextPosition *beginning = [textField beginningOfDocument];
[textField setSelectedTextRange:[textField textRangeFromPosition:beginning
toPosition:beginning]];
}

在头文件或实现的类扩展中指定很重要 <UITextFieldDelegate>并如上所示设置委托(delegate)。

关于ios - UIAlertView 中的 UITextField 无法设置光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19457385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com