正在将TextField添加到UIAlertView



我需要将TextField添加到UIAlertView。我知道苹果公司不鼓励这种做法。那么,有什么库可以用来将TextField添加到类似UIAlertView的帧中吗?

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

此外,您还需要实现UITextFieldDelegateUIAlertViewDelegate协议。

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

UIAlertViewStyleDefault
UIAlertViewStyleSecureTextInput
UIAlertViewStylePlainTextInput
UIAlertViewStyleLoginAndPasswordInput

UIAlertViewStylePlainTextInput就是你想要的。

苹果强烈反对与上述视图层次相混淆。

我使用BlockAlertsAndActionSheets而不是AlertViews和ActionSheets的Apple组件,因为我更喜欢块方法。在源中还包含一个BlockTextPromptAlertView,这可能是您想要的。您可以替换该控件的图像以恢复苹果风格。

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
}

试试这样的东西:

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Title"
                                                 message:@"nn"
                                                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];

自iOS 8起,UIAlertView已被弃用,取而代之的是UIAlertController,它增加了对使用以下方法添加UITextField的支持:

- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;

请参阅此答案以获取示例。

您可以尝试:

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Your title here!" message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:testTextField];
[myAlertView show];
[myAlertView release];

请点击此链接了解详细信息。

首先将UIAlertViewDelegate添加到ViewController.h中类似的文件

#import <UIKit/UIKit.h>
@interface UIViewController : UITableViewController<UIAlertViewDelegate>
@end

然后添加下面的代码,你想提醒显示,

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
                                            message:@"Message"
                                           delegate:self
                                  cancelButtonTitle:@"Done"
                                  otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];

它的委托方法返回UItextField 的输入

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%@", [alertView textFieldAtIndex:0].text);
}

添加到"Shmidt"的答案中,捕获在UIAlertView中输入的文本的代码粘贴在下面(感谢"Wayne Hartman"从UIAlertView获取文本)

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        self.userNumber = [alertView textFieldAtIndex:0].text;
        if (self.userNumber) {
            // user enetered value
            NSLog(@"self.userNumber: %@",self.userNumber);
        } else {
            NSLog(@"null");
        }
    }
}

请参阅。。。http://iosdevelopertips.com/undocumented/alert-with-textfields.html这是一个私有api,如果你在appstore中的应用程序中使用它,它可能会被拒绝,但它对企业开发来说很好

最新更新