将TextField添加到UIAlertView

时间:2012-04-01 06:58:54

标签: iphone objective-c ios cocoa-touch

我需要向TextField添加UIAlertView。我知道苹果不鼓励这种做法。那么我可以利用任何库将TextField添加到UIAlertView外观相似的框架中吗?

9 个答案:

答案 0 :(得分: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];

此外,您需要实施UITextFieldDelegateUIAlertViewDelegate协议。

答案 1 :(得分:14)

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

UIAlertViewStyleDefault
UIAlertViewStyleSecureTextInput
UIAlertViewStylePlainTextInput
UIAlertViewStyleLoginAndPasswordInput

UIAlertViewStylePlainTextInput是你想要的人。

Apple强烈建议不要使用上述视图层次结构。

答案 2 :(得分:6)

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

Project on gitgub

Tutorial which gets you started

示例:

- (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
}

答案 3 :(得分: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];

答案 4 :(得分:4)

自iOS 8开始UIAlertView已被弃用,而UIAlertController支持使用该方法添加UITextField的支持:

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

有关示例,请参阅this answer

答案 5 :(得分:3)

您可以尝试:

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

请按照此link了解详细信息。

答案 6 :(得分:1)

首先将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);
}

答案 7 :(得分:0)

请参阅此... http://iosdevelopertips.com/undocumented/alert-with-textfields.html 这是私有API,如果您在appstore中将其用于app,则可能会遭到拒绝,但对于企业开发来说这很好。

答案 8 :(得分:0)

添加来自Shmidt&#39;的回答,捕获在UIAlertView中输入的文本的代码粘贴在下面(谢谢你&#39; Wayne Hartman&#39; Getting text from 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");
        }

    }
}