在iPhone中显示带文本框的alertview

Mug*_*nth 7 iphone cocoa-touch

是否可以像AppStore应用程序一样显示带有文本框的警报视图.它在这样的对话框中要求输入密码.我已经看过至少使用它的其他几个第三方应用程序.它是私有API吗?

小智 16

以下是Tharindu Madushana的"Apple Approved"方式.我从这个页面的评论中得到了它:http://iosdevelopertips.com/undocumented/alert-with-textfields.html

// Ask for Username and password.
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Twitter Details!" message:@"\n \n \n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

// Adds a username Field
UITextField *utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; 
utextfield.placeholder = @"Username";
[utextfield setBackgroundColor:[UIColor whiteColor]]; 
[alertview addSubview:utextfield];

// Adds a password Field
UITextField *ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 80.0, 260.0, 25.0)]; 
ptextfield.placeholder = @"Password";
[ptextfield setSecureTextEntry:YES];

[ptextfield setBackgroundColor:[UIColor whiteColor]]; [alertview addSubview:ptextfield];
// Move a little to show up the keyboard
CGAffineTransform transform = CGAffineTransformMakeTranslation(0.0, 80.0);
[alertview setTransform:transform];

// Show alert on screen.
[alertview show];
[alertview release];

//...
// Don't forget to release these after getting their values
[utextfield release];
[ptextfield release];
Run Code Online (Sandbox Code Playgroud)

最后得到文本

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0)
        return; //Cancel

    UITextField *field = (UITextField *)[[alertView subviews] lastObject];
    NSLog (@"%@", field.text);
}
Run Code Online (Sandbox Code Playgroud)


Jan*_*les 9

是的,没有证件.要向UIAlertView添加文本字段,请使用addTextFieldWithValue:label:方法.使用默认文本作为第一个参数调用,将显示在空字段中的文本作为第二个参数调用.完成后,您可以使用textFieldAtIndex:n访问该字段 - 见下文.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Who are you?"     
                       message:@"Give your full name" 
                       delegate:self  cancelButtonTitle:@"Cancel"   
                       otherButtonTitles:@"OK", nil]; 
[alert addTextFieldWithValue:@""label:@"Name"]; 

// Customise name field 
UITextField* name = [alert textFieldAtIndex:0]; 
name.clearButtonMode = UITextFieldViewModeWhileEditing; 
name.keyboardType = UIKeyboardTypeAlphabet; 
name.keyboardAppearance = UIKeyboardAppearanceAlert; 
[alert show]; 
Run Code Online (Sandbox Code Playgroud)

下一个代码段显示了如何在名称字段中检索值:

NSLog("Name is %@", [[modalView textFieldAtIndex:0] text]);
Run Code Online (Sandbox Code Playgroud)

  • 正如其他人已经提到过的,现在这会让你从应用程序商店中被踢出去. (5认同)

Hal*_*alR 5

这是一个非常古老的问题,有很老的答案.

这是我从ios 5开始如何将UITextfield放入UIAlertView的示例:

   UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"New List Name" message:@"" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Continue", nil];

    message.alertViewStyle = UIAlertViewStylePlainTextInput;
    self.alertTextField = [message textFieldAtIndex:0];
    self.alertTextField.keyboardType = UIKeyboardTypeAlphabet;
    message.delegate = self;
    [message show];
    [self.alertTextField becomeFirstResponder];
Run Code Online (Sandbox Code Playgroud)

alertTextField的设置方式如下:

@property (nonatomic, strong) UITextField *alertTextField;
Run Code Online (Sandbox Code Playgroud)