我有一个项目已经运作了好几年了。突然,当我升级到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];
}
}
答案 0 :(得分: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不支持向UIAlertView
添加子视图,因此您应该使用支持的方式代替:)
答案 1 :(得分:2)
尝试将您的alertview代码移至viewWillAppear。 UI未必在viewDidLoad中初始化。