如何在UIAlertView中初始化文本字段

tim*_*iew 29 iphone uialertview ios ios5

更新UIAlertView现在有一个样式,允许在UIAlertViewie中的文本输入字段

alert.alertViewStyle = UIAlertViewStylePlainTextInput;
Run Code Online (Sandbox Code Playgroud)

这很好但我想用一个像"sample"这样的defualt文本初始化输入文本.

我看到过去的人们都使用过无证的api(效果很好)

[alert addTextFieldWithValue:@"sample text" label:@"Text Field"];
Run Code Online (Sandbox Code Playgroud)

但由于这仍然不是官方的Apple公共API,我无法使用它.

还有其他办法吗?我确实尝试初始化,willPresentAlertView但文本字段似乎是只读的.

谢谢

Mut*_*tix 57

UIALertView有一个textFieldAtIndex:返回的方法,UITextField你要的对象.

对于a UIAlertViewStylePlainTextInput,textfield的索引为0.

然后,您可以设置文本字段的占位符(或文本)属性:

UIAlertView *alert = ....
UITextField *textField = [alert textFieldAtIndex:0];
textField.placeholder = @"your text";
Run Code Online (Sandbox Code Playgroud)

UIAlertView类参考


Dip*_*jak 9

简单的方法

 UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"your title" message:@"your message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    alerView.alertViewStyle = UIAlertViewStylePlainTextInput;
    [[alerView textFieldAtIndex:0] setPlaceholder:@"placeholder text..."];
    [alerView show];
Run Code Online (Sandbox Code Playgroud)


Ste*_*ton 8

没有测试,但我认为这会工作:

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:...];
UITextField* textField = [alert textFieldAtIndex:0];
textField.text = @"sample";
[alert show];
Run Code Online (Sandbox Code Playgroud)


Sal*_*har 6

要在uiAlertView中设置默认值,这个东西会起作用.

UIAlertView *alert = ....
UITextField *textField = [alert textFieldAtIndex:0];
[textField setText:@"My default text"];
[alert show];
Run Code Online (Sandbox Code Playgroud)