如何将 TextField 和 CheckBox 添加到同一个 AlertView

How can I Add TextField & CheckBox into the same AlertView

我已经将 CheckBox 添加到 AlertView 现在我想将 TextField 也添加到同一个 AlertView 中。

UIAlertView *alertForExistingCredit = [[UIAlertView alloc] init];         
rememberButton = [[UIButton alloc] init];
UIView *v = [[UIView alloc] init];
[rememberButton setImage:[UIImage imageNamed:@"PortraitPhoto_Checkbox-Normal.png"]
forState:UIControlStateNormal];
[rememberButton setTitle:@"Save Code" forState:UIControlStateNormal];
[rememberButton addTarget:self action:@selector(toggleRememberMethod) forControlEvents:UIControlEventTouchUpInside];            
alertForExistingCredit = [[UIAlertView alloc] initWithTitle:@"PrePay Code"
    message:@""
    delegate:self
    cancelButtonTitle:kCancelMessage
    otherButtonTitles:kAlertOkMessage, nil];
[v addSubview:rememberButton];

如果我使用:

//plain text part
//            alertForExistingCredit.alertViewStyle = UIAlertViewStylePlainTextInput;
//            [[alertForExistingCredit textFieldAtIndex:0] setPlaceholder:@"PrePay Code"]; this lines of code it is not showing on the screen

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        v.frame = CGRectMake(0, 0, 250, 40);
       rememberButton.frame = CGRectMake(0.0, 0, 250, 50.0);
        [rememberButton setTitleColor:[UIColor blackColor]
                             forState:UIControlStateNormal];
        alertForExistingCredit.message = @"Please insert your PrePay code";
        [alertForExistingCredit setValue:v  forKey:@"accessoryView"]
    }else {
        v.frame = CGRectMake(0, 80, 250, 40);
        rememberButton.frame = CGRectMake(0.0, 0, 250, 40.0);
        alertForExistingCredit.message = @"Please insert your PrePay code";
        [alertForExistingCredit addSubview:v];
    }
    alertForExistingCredit.tag = 0;
    [alertForExistingCredit show];
}

添加一个子视图到您的 UIAlertView 并将您的按钮放入该视图,请参阅代码以将 UIView 添加到您的 UIAlertView

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TEST" message:@"subview" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
[alert setValue:view forKey:@"accessoryView"];
// add your button in *view*.
[alert show];

尝试将此 UITextField 添加到您的 v (UIView),并尝试调整框架大小和位置。

        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 250, 50)];
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.font = [UIFont systemFontOfSize:15];
        textField.placeholder = @"enter text";
        textField.autocorrectionType = UITextAutocorrectionTypeNo;
        textField.keyboardType = UIKeyboardTypeDefault;
        textField.returnKeyType = UIReturnKeyDone;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        textField.delegate = self;
        [v addSubview:textField];