将TextField添加到UIAlertView

sha*_*jem 21 iphone cocoa-touch objective-c ios

我需要一个补充TextFieldUIAlertView.我知道苹果不鼓励这种做法.那么,有没有,我可以利用的添加任何图书馆TextFieldUIAlertView外观相似框架?

Shm*_*idt 69

对于iOS5:

UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Title" message:@"Please enter someth" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
[av textFieldAtIndex:0].delegate = self;
[av show];
Run Code Online (Sandbox Code Playgroud)

此外,您将需要实现UITextFieldDelegate,UIAlertViewDelegate协议.


小智 14

不幸的是,唯一的官方API是iOS 5及更高版本,它是一个名为的属性alertViewStyle,可以设置为以下参数:

UIAlertViewStyleDefault
UIAlertViewStyleSecureTextInput
UIAlertViewStylePlainTextInput
UIAlertViewStyleLoginAndPasswordInput
Run Code Online (Sandbox Code Playgroud)

UIAlertViewStylePlainTextInput 成为你想要的人.

Apple强烈反对与上述视图层次结构混淆.


And*_*ese 6

我正在使用BlockAlertsAndActionSheets而不是AlertViews和ActionSheets的Apple组件,因为我更喜欢块方法.还包含一个BlockTextPromptAlertView源代码,可能是你想要的.您可以替换该控件的图像以获得Apple风格的背面.

关于gitgub的项目

让你入门的教程

例:

- (IBAction)newFolder:(id)sender {
    id selfDelegate = self;
    UITextField                 *textField;
    BlockTextPromptAlertView    *alert = [BlockTextPromptAlertView  promptWithTitle :@"New Folder"
                                                                    message         :@"Please enter the name of the new folder!"
                                                                    textField       :&textField];
    [alert setCancelButtonWithTitle:@"Cancel" block:nil];
    [alert addButtonWithTitle:@"Okay" block:^{
        [selfDelegate createFolder:textField.text];
    }];
    [alert show];
}

- (void)createFolder:(NSString*)folderName {
    // do stuff
}
Run Code Online (Sandbox Code Playgroud)


fan*_*ard 4

尝试这样的事情:

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Title"
                                                 message:@"\n\n"
                                                delegate:self
                                       cancelButtonTitle:@"Cancel"
                                       otherButtonTitles:@"Save", nil] autorelease];
CGRect rect = {12, 60, 260, 25};
UITextField *dirField = [[[UITextField alloc] initWithFrame:rect] autorelease];
dirField.backgroundColor = [UIColor whiteColor];
[dirField becomeFirstResponder];
[alert addSubview:dirField];

[alert show];
Run Code Online (Sandbox Code Playgroud)

  • 我认为苹果不鼓励你的做法。 (2认同)