如何将 TextField 和 CheckBox 添加到同一个 AlertView
Posted
技术标签:
【中文标题】如何将 TextField 和 CheckBox 添加到同一个 AlertView【英文标题】:How can I Add TextField & CheckBox into the same AlertView 【发布时间】:2015-04-17 08:39:35 【问题描述】:我已将 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
的文档中明确指出的那样,您不应更改视图层次结构。因此,您需要创建自己的视图来显示并且不应该使用UIAlertView
使用自定义视图,可能还有来自 github 的第 3 方警报视图
试试this可能对你有帮助
你们还有其他解决方案吗?
请分享预期输出的屏幕截图
【参考方案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];
【讨论】:
【参考方案2】:在您的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];
【讨论】:
以上是关于如何将 TextField 和 CheckBox 添加到同一个 AlertView的主要内容,如果未能解决你的问题,请参考以下文章
Java AWT 图形界面编程AWT 常用 Component 组件 ( Frame | Label | Checkbox | List | Choice | TextField )