我有一个运作良好的程序几年。突然,当我升级到iOS 7时,它不再起作用。我在UIAlertView内的RootViewController中放置了一个UIAlertView(密码对话框)。将显示UIAlertView,但其中不显示UITextField。任何关于为什么突然不起作用的线索?

缩写代码:

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Some initialization

   if (!firstTimeInit) {
     alert =
        [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Password",
                                                             @"Password")
                                   message:NSLocalizedString(@"EnterPassword",
                                                             @"EnterPassword")
                                  delegate:self
                         cancelButtonTitle:nil
                         otherButtonTitles:@"OK", nil];
     alert.frame = CGRectMake( 0, 0, 300, 260);

     UITextField *myTextField =
         [[UITextField alloc] initWithFrame:CGRectMake(32.0f, 75.0f,
                                                       220.0f, 28.0f)];
     myTextField.placeholder = NSLocalizedString(@"Password", @"Password");

     [myTextField setSecureTextEntry:YES];
     [myTextField setBackgroundColor:[UIColor whiteColor]];
     [myTextField setBorderStyle:UITextBorderStyleBezel];
     myTextField.tag = 11;
     [alert addSubview:myTextField];
     CGAffineTransform myTransform =
         CGAffineTransformMakeTranslation(0.0, 75.0);
     [alert setTransform:myTransform];

     [prefs retain];
     [alert show];
     [myTextField becomeFirstResponder];
  }
}

最佳答案

您应该使用UIAlertViewStyleSecureTextInput文本字段。

UIAlertView *alert =
    [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Password",
                                                         @"Password")
                               message:NSLocalizedString(@"EnterPassword",
                                                         @"EnterPassword")];
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;


在您的委托方法中执行以下操作:

- (void)alertView:(UIAlertView *)alertView
    didDismissWithButtonIndex:(NSInteger)buttonIndex
{
  NSString *password = [[alertView textFieldAtIndex:0] text];
  // Whatever password processing.

}


UIKit不支持将子视图添加到UIAlertView,因此您应该使用受支持的方式来执行此操作:)

关于ios - RootViewController的viewDidLoad中的UIAlertView在iOS 7中突然不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18926989/

10-13 09:32