【问题标题】:Unable to add UITextField to UIAlertView on iOS7...works in iOS 6无法在 iOS7 上将 UITextField 添加到 UIAlertView ...在 iOS 6 中有效
【发布时间】:2013-09-04 03:55:55
【问题描述】:

以下代码适用于 iOS6(及之前),但 UITextField 不显示在 iOS7 中...关于如何让 UITextField 在 iOS7 中的 UIAlterView 中显示的任何想法?

UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter ESC Score"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
dialog.tag = 5;

nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setKeyboardType:UIKeyboardTypeNumberPad];
[nameField becomeFirstResponder];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];

[nameField release];

为 iOS6 运行的代码显示如下:

iOS7 中的相同代码显示了这一点(注意 UITextField 是如何丢失并且没有键盘):

【问题讨论】:

  • iOS7 仍是beta,你到底期待什么?
  • 嗯...我猜如果 Apple 将在 9 月中旬发布 iOS7,我希望能够与一个才华横溢、聪明且志同道合的开发者社区合作,以便我们的应用程序是对成千上万的现有 Apple 用户来说并非无法使用。苹果不指望任何人谈论如何积极主动地让用户满意,这似乎有点愚蠢。所以我们要假装iOS7在正式发布之前不存在吗? NDA 很重要,但这似乎很愚蠢……甚至更愚蠢的是,有人花时间处理 iOS7 问题和人们的叮当声。只是说...
  • Apple 提供了一个讨论 iOS7 的论坛,在该论坛中可以讨论禁运的 API。此处关于未发布和可能无法运行的软件的讨论对其他人来说并不是非常有用 - 除非非常特别地标记了版本号
  • 难以置信,不得不为 ios 7 安装全新的 osx,难以置信!
  • 猜猜这个问题是如何进入 iOS 7 的公开版本的,我们的应用程序在这方面也被破坏了。任何使用这种技术的应用程序(实际上并不少见)都被破坏了。对不起,伙计们,但回答某事“在 NDA 下,对其他人无用”是维持一个富有成效的社区的完全错误的方法,该社区应该在现实世界的问题上互相帮助,这绝对是。 Beta 版与否,这些问题都存在 - 因此,请对此持建设性态度,而不是轻视或不为问题增加任何价值。

标签: objective-c ios7 uitextfield uialertview uialertviewdelegate


【解决方案1】:

在 iOS 7 中,您无法轻松更改 UIAlertView 的视图层次结构。(您也不应该这样做;文档明确告诉您不要这样做。)前往开发者论坛查看有关它的长时间讨论。

在您的情况下,另一种选择是设置alert.alertViewStyle = UIAlertViewStylePlainTextInput; 这将为您添加一个文本字段。您可以使用UITextField *textField = [alertView textFieldAtIndex:0]; 在 UIAlertView 委托回调中访问它。

【讨论】:

  • 这适用于单个文本字段。如果我想向 uialertview 添加两个文本字段,那么解决方案是什么?
  • @loganathan UIAlertViewStyleLoginAndPasswordInput。如果您不想遮挡第二个框,您可以设置[alertView textFieldAtIndex:1].secureTextEntry = NO。对于比这更复杂的任何事情,请自行查看。
  • @AaronBrager 有意义 :-)
  • @AaronBrager,但它会在应用审查时被苹果接受吗?
  • 3 个文本字段呢?我们将其用于密码重置功能:“当前密码、新密码、新密码”。
【解决方案2】:

@Aaron Brager 有正确的解决方案。此外,我在他建议默认数字键盘之后添加了一行。

UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter ESC Score"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
dialog.tag = 5;

dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
[dialog textFieldAtIndex:0].keyboardType = UIKeyboardTypeNumberPad;

CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];

【讨论】:

  • 遵循@Aaron Brager 的建议并更改警报的配置方式......就像 iTrout 为我做了诀窍!谢谢你们!!!
【解决方案3】:

1) 在方法中 - (id)initWithAlertTitle:(NSString *)title checkForPassword:(NSString *)password
你应该添加

self.alertViewStyle = UIAlertViewStylePlainTextInput;

样本:

(id)initWithAlertTitle:(NSString *)title
        checkForPassword:(NSString *)password{
     if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
     {
    self.alertViewStyle = UIAlertViewStylePlainTextInput;
    }
    self = [super initWithTitle:title
                        message:@"" // password field will go here
                       delegate:self
              cancelButtonTitle:@"Cancel"
              otherButtonTitles:@"Enter", nil];
    if (self) {
        self.password = password;
        self.hashTechnique = HashTechniqueNone; // use no hashing by default
        secondMessage = @"Please Enter New Password";
        thirdMessage = @"Please Re-Enter Password";
        secondMessageNew = @"Please Enter Password";
    }

NSLog(@" _password_ %@",_password);
NSLog(@"_old_password_ %@",[[NSUserDefaults standardUserDefaults] objectForKey:kPassword]);

return self;
}

在方法中显示 添加下一个

(void)show {

    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
     {

       UITextField *passwordField = [self textFieldAtIndex:0];
       passwordField.delegate = self;
      self.passwordField = passwordField;
    }
   else
   {
               UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(14, 45, 256, 25)];
               passwordField.secureTextEntry = YES;
               passwordField.placeholder = @"";
               passwordField.backgroundColor = [UIColor whiteColor];



               // Pad out the left side of the view to properly inset the text
               UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 6, 19)];
                passwordField.leftView = paddingView;
                passwordField.leftViewMode = UITextFieldViewModeAlways;

           //    // Set delegate
           self.passwordField.delegate = self;

           // Set as property

            self.passwordField = passwordField;
           // Add to subview
           [self addSubview:_passwordField];
     }

    // Show alert
   [super show];

}

也对方法点击进行更改

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {


    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        UITextField *passwordField = [self textFieldAtIndex:0];
        self.passwordField = passwordField;
    }

    if (buttonIndex == alertView.firstOtherButtonIndex) {

        if ([self enteredTextIsCorrect] || [self.title isEqualToString:secondMessage] || [self.title isEqualToString:secondMessageNew]) {

            if (([self.title isEqualToString:secondMessage] || [self.title isEqualToString:secondMessageNew]) && (self.passwordField.text.length > 0)) {
                self.password = self.passwordField.text;
                self.title = thirdMessage;
                self.passwordField.text = @"";

                if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
                {
                    if ([self.passwordDelegate respondsToSelector:@selector(notifyParent::)]) {
                        [self.passwordDelegate notifyParent:thirdMessage:self.password];
                    }
               }
            }else
            {
                if ([self.title isEqualToString:thirdMessage]) {
                    [[NSUserDefaults standardUserDefaults] setObject:self.password forKey:kPassword];
                    [[NSUserDefaults standardUserDefaults] synchronize];

                    if (self.passwordDelegate) {
                        if ([self.passwordDelegate respondsToSelector:@selector(notifyParentWithState:)]) {
                            [self.passwordDelegate notifyParentWithState:YES];
                        }
                    }
                }else{
                    if ([self.title isEqualToString:secondMessageNew]) {
                        self.title = secondMessageNew;
                    }
                    else{
                        self.title = secondMessage;
                    }

                     self.passwordField.text = @"";
                     if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
                     {
                         if ([self.passwordDelegate respondsToSelector:@selector(notifyParent::)]) {
                             [self.passwordDelegate notifyParent:self.title:self.password];
                        }
                    }
                }
            }
        }

        // If incorrect then animate
        else {
            [self animateIncorrectPassword];
        }
    }
}

【讨论】:

  • 我的解决方案适用于 iOS 5 及更高版本(您仅在 iOS 7 及更高版本上使用它)。除非您支持 iOS 4,否则不需要这种骇客。此外,您从中复制的任何应用程序都会在 NSUserDefaults 中保存密码,这是一个糟糕的主意。
【解决方案4】:
UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Credit Card Number"
                                  message:@"Please enter your credit card number:"
                                  delegate:self
                                  cancelButtonTitle:@"Cancel"
                                  otherButtonTitles:@"Ok", nil];
        [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        /* Display a numerical keypad for this text field */
        UITextField *textField = [alertView textFieldAtIndex:0];
        textField.keyboardType = UIKeyboardTypeNumberPad;

[alertView show];

【讨论】:

    【解决方案5】:

    您还可以查看 cocoacontrols.com 上的自定义控件。看看MLAertView(ios 7 like UI) 和TSAlertView(ios 6 like UI)。它们也可以转换为旋转角度。

    【讨论】:

      【解决方案6】:

      就像魅力一样工作

      所有 iOS 版本的 UIAlertView 中的两个 UITextField

      -(IBAction) showAlertView {
      
          UIAlertView *alert;  
          UITextField *callForwardNumber;
          UItextField *callForwardCondition;
      
          alert = [[UIAlertView alloc] initWithTitle:@"Enter Phone Number & Rule"
                                         message:@""
                                        delegate:self
                               cancelButtonTitle:@"Cancel"
                               otherButtonTitles:@"Save", nil];
      
          //alert.transform = CGAffineTransformMakeTranslation(0, 110);
      
          callForwardNumber = [[UITextField alloc] init];
          callForwardNumber.keyboardType = UIKeyboardTypeNumberPad;
          callForwardNumber.text = [R.prefs objectForKey:@"fwd_number"];
          callForwardNumber.borderStyle = UITextBorderStyleRoundedRect;
          callForwardNumber.delegate = self;
          callForwardNumber.tag = 1;
      
          callForwardCondition = [[UITextField alloc] init];
          callForwardCondition.text = callCondition;
          callForwardCondition.borderStyle = UITextBorderStyleRoundedRect;
          callForwardCondition.delegate = self;
          callForwardCondition.tag = 2;
          [callForwardCondition setKeyboardType:UIKeyboardTypeNumberPad];
      
      if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
          UIView* customAccessory = 
                         [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 55)];
          callForwardNumber.frame = CGRectMake(0, 0, 245.0, 25.0);
          callForwardCondition.frame = CGRectMake(0, 30.0, 245.0, 25.0);
          [customAccessory addSubview:callForwardNumber];
          [customAccessory addSubview:callForwardCondition];
          [alert setValue:customAccessory forKey:@"accessoryView"];
          [alert show];
      } else {
          alert.message = @"\n\n\n";
          [alert show];
          callForwardNumber.frame = CGRectMake(20.0, 45.0, 245.0, 25.0);
          callForwardCondition.frame = CGRectMake(20.0, 75.0, 245.0, 25.0);
          [alert addSubview:callForwardNumber];
          [alert addSubview:callForwardCondition];
      }
      
      }
      

      【讨论】:

        【解决方案7】:

        我也面临同样的问题,在冲浪期间,我得到了对我有用的答案。我希望它也对你有用。

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Folder Name?" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
        alertView.tag = 2;
        alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
        [alertView show];
        
        - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex    {
            UITextField * alertTextField = [alertView textFieldAtIndex:0];
            NSLog(@"alerttextfiled - %@",alertTextField.text);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-09-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多