0

所以我想向用户展示一个警报视图,让他们输入密码。我想检查以确保在键盘上输入了某些内容。我知道在 UIAlertViewDelegate 中,您可以获得文本输入。但是,到目前为止,我唯一的解决方案是,

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"buttonIndex: %i", buttonIndex);

    if (buttonIndex == 0) {
        UITextField *passwordTextField = [alertView textFieldAtIndex:0];
        if (!passwordTextField.text.length == 0) {
            NSLog(@"password: %@", passwordTextField.text);
            // save the password
        }
        else {
            // Show the alert view again asking for the password
        }

    }

}

如果他们没有输入任何内容,我会在他们单击“确定”后立即再次要求输入密码。有没有更好的解决方案?谢谢!

4

2 回答 2

0

再次显示警报视图。您可以将警报视图放在一个方法中并调用它。

...

} else {

    [self showPassAlert];

}

您还可以查看文本字段何时被编辑,并在警报视图中启用/禁用按钮。如果文本字段的长度为 0,则禁用该按钮。当文本字段被编辑并且其长度大于 0 时,启用该按钮。这比关闭和重新打开视图更优雅。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextFieldTextDidChangeNotification object:textField];

...

-(void)textDidChange {

    for (UIView *view in alertView.subviews) {

        if ([view isKindOfClass:[UIButton class]]) {

            if (textField.text.length == 0) {

                //you may need to look for the button's title to make sure you are not disabling the cancel button

                ((UIButton *) view).enabled = NO;

            } else {

                ((UIButton *) view).enabled = YES;

            }

        }

    }

}
于 2012-08-30T16:36:46.967 回答
0

为什么不iboutlet为您的 UITextFields 创建 /reference,这将帮助您随时检索其值。

于 2013-10-11T05:11:52.317 回答