ios 将 TextField 添加到 UIAlertView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9962538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:39:09  来源:igfitidea点击:

Adding TextField to UIAlertView

iphoneobjective-cioscocoa-touch

提问by shajem

I need to add a TextFieldto an UIAlertView. I understand that apple discourage this approach. So is there any library that i could make use of to add a TextFieldto a UIAlertViewlook-alike frame ?

我需要将 a 添加TextFieldUIAlertView. 我知道苹果不鼓励这种方法。那么是否有任何库可以用来将 a 添加TextFieldUIAlertView相似的框架中?

回答by Shmidt

For iOS5:

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

Also, you 'll need to implement UITextFieldDelegate, UIAlertViewDelegateprotocols.

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

回答by Brandon Tennant

Unfortunately the only official API for this is iOS 5 and up, it's a property called alertViewStylewhich can be set to the following parameters:

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

UIAlertViewStyleDefault
UIAlertViewStyleSecureTextInput
UIAlertViewStylePlainTextInput
UIAlertViewStyleLoginAndPasswordInput

UIAlertViewStylePlainTextInputbeing the one you want.

UIAlertViewStylePlainTextInput成为你想要的那个。

Messing with the view hierarchy as described above is strongly discouraged by Apple.

Apple 强烈不鼓励使用上述视图层次结构。

回答by Andy Friese

I'm using BlockAlertsAndActionSheetsinstead of the Apple components for AlertViews and ActionSheets as I prefer the blocks-approach. Also contains a BlockTextPromptAlertViewin the source, which might be what you want. You can replace the images of that control to get the Apple-style back.

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

Project on gitgub

gitgub 上的项目

Tutorial which gets you started

入门教程

Example:

例子:

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

回答by fannheyward

Try something like this:

尝试这样的事情:

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

回答by Ric Santos

As of iOS 8 UIAlertViewhas been deprecated in favor of UIAlertController, which adds support for adding UITextFields using the method:

从 iOS 8UIAlertView开始,已弃用UIAlertController,这增加了对UITextField使用以下方法添加s 的支持:

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

See this answerfor an example.

有关示例,请参阅此答案

回答by Abdullah Md. Zubair

You can try:

你可以试试:

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

Follow this linkfor detail.

详情请点击链接

回答by Vijay Rathod

first of All Add UIAlertViewDelegate into ViewController.h File like

首先将 UIAlertViewDelegate 添加到 ViewController.h 文件中,例如

#import <UIKit/UIKit.h>

@interface UIViewController : UITableViewController<UIAlertViewDelegate>

@end

and than Add Below Code where you wants to alert Display,

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

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

and it's delegate method which returns what input of UItextField

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

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

回答by Ankit Srivastava

refer this... http://iosdevelopertips.com/undocumented/alert-with-textfields.htmlthis is private api and if you use it for app in appstore it might get rejected but it is fine for enterprise development.

参考这个... http://iosdevelopertips.com/undocumented/alert-with-textfields.html这是私有 api,如果你将它用于 appstore 中的应用程序,它可能会被拒绝,但它适用于企业开发。

回答by tmr

adding to answer from 'Shmidt', the code to capture text entered in UIAlertView is pasted below (thank you 'Wayne Hartman' Getting text from UIAlertView)

添加到来自'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");
        }

    }
}