【问题标题】:Adding TextField to UIAlertView将 TextField 添加到 UIAlertView
【发布时间】:2012-04-01 06:58:53
【问题描述】:

我需要将TextField 添加到UIAlertView。我知道苹果不鼓励这种方法。那么有没有我可以用来将TextField 添加到UIAlertView 相似框架的库?

【问题讨论】:

    标签: iphone objective-c ios cocoa-touch


    【解决方案1】:

    对于 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 协议。

    【讨论】:

      【解决方案2】:

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

      UIAlertViewStyleDefault
      UIAlertViewStyleSecureTextInput
      UIAlertViewStylePlainTextInput
      UIAlertViewStyleLoginAndPasswordInput
      

      UIAlertViewStylePlainTextInput 是你想要的。

      Apple 强烈不鼓励像上面描述的那样混淆视图层次结构。

      【讨论】:

      • 我必须为 iOS 4 和 5 创建这个。所以你知道我可以使用的任何其他库
      【解决方案3】:

      我使用BlockAlertsAndActionSheets 而不是 Apple 组件用于 AlertViews 和 ActionSheets,因为我更喜欢块方法。源中还包含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
      }
      

      【讨论】:

      • 你能给我看一个使用BlockTextPromptAlertView 的例子吗?我的印象是不允许开发人员向 UIAlertView 添加任何文本字段等。
      • 你的印象是对的。这就是 BlockAlertsAndActionSheets使用 UIAlertView 的原因,它是一个完全独立的实现,没有使用任何私有 API。我现在无法访问我的资源,但可以在几个小时内使用代码示例更新我的答案。
      • 有没有办法让我停止动画? (Th 弹跳效果)
      • 如果您想更改控件的动画方式,请尝试在 BlockAlertView.m 和 BlockTextPromptAlertView.m 中搜索 animateWithDuration
      【解决方案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];
      

      【讨论】:

      • 我认为 Apple 不鼓励你的做法。
      【解决方案5】:

      从 iOS 8 开始,UIAlertView 已被弃用,取而代之的是 UIAlertController,这增加了对使用以下方法添加 UITextFields 的支持:

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

      有关示例,请参阅 this answer

      【讨论】:

        【解决方案6】:

        你可以试试:

        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了解详情。

        【讨论】:

        • 是的,您的代码有效。但 Apple 不鼓励这种方法,因为它使用私有 API。那么有没有其他方法可以解决这个问题?
        • @anonymous OP 引用了文档中的那句话:该类的视图层次结构是私有的,不得修改。
        【解决方案7】:

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

        【讨论】:

          【解决方案8】:

          添加到来自“Shmidt”的答案,捕获在 UIAlertView 中输入的文本的代码粘贴在下面(感谢“Wayne Hartman”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");
                  }
          
              }
          }
          

          【讨论】:

            【解决方案9】:

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

            【讨论】:

            • 感谢您的回复,但我希望将我的应用发布到应用商店:S
            • 我不建议你实现这个。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-01-19
            • 2011-07-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多