如何添加TextField& CheckBox进入同一个AlertView

时间:2015-04-17 08:39:36

标签: ios checkbox uiview uialertview textfield

我已将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];
}

2 个答案:

答案 0 :(得分:1)

添加子视图添加到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];

答案 1 :(得分:1)

尝试将此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];