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

Iphone 如何在UIAlertView中初始化文本字段,iphone,ios,ios5,uialertview,Iphone,Ios,Ios5,Uialertview,更新后的UIAlertView现在有一种样式,允许在UIAlertView中输入文本字段,即 alert.alertViewStyle = UIAlertViewStylePlainTextInput; 这很好,但我想用一个像“sample”这样的不确定文本初始化输入文本 我看到过去的人们使用了未记录的api,比如(非常有效) 但由于这仍然不是苹果官方的公共API,我不能使用它 还有别的办法吗?我确实尝试在willPresentAlertView中初始化,但文本字段似乎是只读的 谢谢未经测试,

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

alert.alertViewStyle = UIAlertViewStylePlainTextInput;
这很好,但我想用一个像“sample”这样的不确定文本初始化输入文本

我看到过去的人们使用了未记录的api,比如(非常有效)

但由于这仍然不是苹果官方的公共API,我不能使用它

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


谢谢

未经测试,但我认为这会起作用:

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:...];
UITextField* textField = [alert textFieldAtIndex:0];
textField.text = @"sample";
[alert show];

UIALertView
有一个
textfieldatinex:
方法,返回所需的
UITextField
对象

对于
UIAlertViewStylePlainTextInput
,文本字段的索引为0

然后可以设置textfield的占位符(或文本)属性:

UIAlertView *alert = ....
UITextField *textField = [alert textFieldAtIndex:0];
textField.placeholder = @"your text";

要在uiAlertView中设置默认值,此功能将起作用

UIAlertView *alert = ....
UITextField *textField = [alert textFieldAtIndex:0];
[textField setText:@"My default text"];
[alert show];
简单的方法

 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];

这似乎不再管用了。textFieldAtIndex生成此错误:**由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因是:“textFieldIndex(0)超出了文本字段数组的界限”。这对我来说很有效,但为什么呢!!!!效果如何反映到警报中!!我不明白@对称,确保设置[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];在尝试获取UITextField之前
- (IBAction)showMessage:(id)sender {
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Add New Category"
                                                      message:nil
                                                     delegate:self 
                                            cancelButtonTitle:@"Add"
                                            otherButtonTitles:@"Cancel", nil];
    [message setAlertViewStyle:UIAlertViewStylePlainTextInput];
    [message show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Add"])
    {
        UITextField *username = [alertView textFieldAtIndex:0];
        NSLog(@"Category Name: %@", username.text);
    }
}
 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];