0

我有一个已经运行了几年的程序。突然,当我升级到 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];
  }
}
4

2 回答 2

3

您应该使用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 不支持向 a 添加子视图UIAlertView,因此您应该使用支持的方式来代替 :)

于 2013-09-20T23:03:23.477 回答
2

尝试将您的 alertview 代码移动到 viewWillAppear。UI 不一定在 viewDidLoad 中初始化。

于 2013-09-21T00:42:52.110 回答