AlertView上的TextField未在iOS 7上显示

时间:2013-08-21 09:07:40

标签: ios uitextfield ios7 uialertview

在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 个答案:

答案 0 :(得分:7)

您希望使用UIAlertView的“new”(iOS 5)方法为您提供UITextField。 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 was never allowed。视图层次结构始终是私有的。 Apple开始执行此限制。

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

答案 1 :(得分: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
    }
}

答案 2 :(得分: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:到警报视图对其视图没有影响。

来自Apple docs

  

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

答案 3 :(得分: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/