4

在 iOS7 之前,我可以使用此代码UITextField向我添加一个(文本输入字段) 。UIAlertView

UITextField *txtNewPassword = [[UITextField alloc] initWithFrame:secondTextFldRect];

        txtNewPassword.delegate     = self;
        txtNewPassword.text         = @"";
        txtNewPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
        txtNewPassword.borderStyle      = UITextBorderStyleRoundedRect;
        txtNewPassword.autocapitalizationType = UITextAutocapitalizationTypeNone;
        txtNewPassword.tag              = kNewPasswordTxtFldTag;
        [txtNewPassword setBackgroundColor:[UIColor whiteColor]];
        [txtNewPassword setKeyboardAppearance:UIKeyboardAppearanceAlert];
        [txtNewPassword setAutocorrectionType:UITextAutocorrectionTypeNo];
        [txtNewPassword setPlaceholder:@"New password"];
        [txtNewPassword setTextAlignment:UITextAlignmentLeft];
        [txtNewPassword setSecureTextEntry:YES];
        [alert addSubview:txtNewPassword];
        [txtNewPassword release];

更新到 iOS7 后它停止工作 - 我的文本字段不再显示。更新我的代码的建议方法是什么?

4

4 回答 4

7
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
dialog.alertViewStyle=UIAlertViewStylePlainTextInput;
[dialog setTitle:@"Your Title"];
[dialog setMessage:@"your message"];

[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"Ok"];


UITextField *_UITextField  = [dialog textFieldAtIndex:0];
_UITextField.placeholder = @"Placeholder";
_UITextField.keyboardType = UIKeyboardTypeEmailAddress;
[dialog show];

//uialertview delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex==1)//OK button 
    {
        //do ur stuff
    }
}
于 2013-08-21T09:24:52.023 回答
7

您想使用为您提供 UITextField 的 UIAlertView 的“新”(i​​OS 5)方法。alertViewStyletextFieldAtIndex:

这将您的代码简化为:

UIAlertView *alert = [[UIAlertView alloc] ...];
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;

UITextField *txtNewPassword = [alert textFieldAtIndex:0];

txtNewPassword.delegate     = self;
txtNewPassword.text         = @"";
txtNewPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
txtNewPassword.tag              = kNewPasswordTxtFldTag;
[txtNewPassword setPlaceholder:@"New password"];

您的代码在 iOS7 上不起作用,因为从不允许向 UIAlertView 添加子视图。视图层次结构一直是私有的。苹果开始强制执行这一限制。

如果你想要一个定制的 UIAlertView 你必须自己写。子类 UIView 并使其看起来像 UIAlertView。

于 2013-08-21T09:19:31.933 回答
3

正如 Matthias Bauch 所建议的,您不需要向警报视图添加文本字段,而是使用UIAlertView属性alertViewStyle. 它接受枚举中定义的值UIAlertViewStyle

typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
    UIAlertViewStyleDefault = 0,
    UIAlertViewStyleSecureTextInput, // Secure text input
    UIAlertViewStylePlainTextInput,  // Plain text input
    UIAlertViewStyleLoginAndPasswordInput // Two text fields, one for username and other for password
};

在您的情况下,要使用此代码,请遵循此代码。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please enter password"
                                                  message:nil
                                                 delegate:self
                                        cancelButtonTitle:@"Cancel"
                                        otherButtonTitles:@"Continue", nil];
[alert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
[alert show];

要验证输入,假设输入的密码必须至少为 6 个字符,请实现此委托方法,

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    NSString *inputText = [[alertView textFieldAtIndex:0] text];
    if( [inputText length] >= 6 )
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

获取用户输入

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Login"])
    {
        UITextField *password = [alertView textFieldAtIndex:0];
        NSLog(@"Password: %@", password.text);
    }
}

UIAlertView具有私有视图层次结构,建议按原样使用它而不进行修改。因此addSubview:,警报视图对其视图没有影响。

来自苹果文档

UIAlertView 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。

于 2013-08-21T09:23:49.950 回答
1

带有 UITextField 的 UIAlertView..

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@" " delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
CGRect frame = CGRectMake(14, 45, 255, 23);
UITextField *textField = [[UITextField alloc] initWithFrame:frame];
textField.placeholder = @"Name";
textField.backgroundColor = [UIColor whiteColor];
textField.autocorrectionType = UITextAutocorrectionTypeDefault;
textField.keyboardType = UIKeyboardTypeAlphabet;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing; // has 'x' button to the right
[alertView addSubview:textField];

[alertView show];

礼貌http://kshitizghimire.com.np/uitextfield-in-uialertview/

于 2013-08-21T09:15:52.703 回答