以编程方式切换到UIAlert控制器

我正在用swift创build一个注册对话框,其中有3个文本字段和一个Switch,并且我成功添加了三个文本字段两个Alert。 以下代码显示相同。

let alertController = UIAlertController(title: "Register", message: "", preferredStyle: .Alert) let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in // ... exit(0) } alertController.addAction(cancelAction) let OKAction = UIAlertAction(title: "Sign UP", style: .Default) { (action) in // ... let name0 = alertController.textFields![0] as UITextField print("Text field: \(name0.text)") let email1 = alertController.textFields![1] as UITextField print("Text field: \(email1.text)") let company2 = alertController.textFields![2] as UITextField print("Text field: \(company2.text)") } alertController.addAction(OKAction) alertController.addTextFieldWithConfigurationHandler { (textField) in textField.placeholder = "Name" textField.keyboardType = .EmailAddress } alertController.addTextFieldWithConfigurationHandler { (textField) in textField.placeholder = "Email" textField.secureTextEntry = false } alertController.addTextFieldWithConfigurationHandler { (textField) in textField.placeholder = "Company" textField.secureTextEntry = false } self.presentViewController(alertController, animated: true) { // ... } 

现在我需要添加一个开关编程到警报视图。我们在Swift2中这样做。 可能吗?我是Swift的新手。

这可能会帮助你。

alertController.addAction(OKAction)之后,在上面的代码中添加此方法调用alertController.view.addSubview(createSwitch()) alertController.addAction(OKAction)

 func createSwitch () -> UISwitch{ let switchControl = UISwitch(frame:CGRectMake(10, 20, 0, 0)); switchControl.on = true switchControl.setOn(true, animated: false); switchControl.addTarget(self, action: "switchValueDidChange:", forControlEvents: .ValueChanged); return switchControl } func switchValueDidChange(sender:UISwitch!){ print("Switch Value : \(sender.on))") } 

输出:

在这里输入图像说明

你可以使用TextField的RightView来添加一个button。 添加一个开关会很好,但是开关不适合TextField高度,也不能改变高度。 为此,您可以添加一个button并使用图像来制作一个TickBox。

带TickBox的AlertController

我已经从一个项目中剥离了这个例子,所以这个例子的图像比下面的要多一点。

在ViewController头添加TextField委托

 @interface CustomTableViewController : UITableViewController <UITextFieldDelegate> 

然后创build您的AlertController并添加TextField

 // create an alert controller UIAlertController *alertWithText = [UIAlertController alertControllerWithTitle:title message:body preferredStyle:UIAlertControllerStyleAlert]; // create the actions handled by each button UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { }]; // add the actions to the alert [alertWithText addAction:action1]; [alertWithText addAction:action2]; // Establish the weak self reference __weak typeof(self) weakSelf = self; [alertWithText addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { // Create button UIButton *checkbox = [UIButton buttonWithType:UIButtonTypeCustom]; [checkbox setFrame:CGRectMake(2 , 2, 18, 18)]; // Not sure about size [checkbox setTag:1]; [checkbox addTarget:weakSelf action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; // Setup image for button [checkbox.imageView setContentMode:UIViewContentModeScaleAspectFit]; [checkbox setImage:[UIImage imageNamed:@"unchecked_checkbox.png"] forState:UIControlStateNormal]; [checkbox setImage:[UIImage imageNamed:@"checked_checkbox.png"] forState:UIControlStateSelected]; [checkbox setImage:[UIImage imageNamed:@"checked_checkbox.png"] forState:UIControlStateHighlighted]; [checkbox setAdjustsImageWhenHighlighted:TRUE]; // Setup the right view in the text field [textField setClearButtonMode:UITextFieldViewModeAlways]; [textField setRightViewMode:UITextFieldViewModeAlways]; [textField setRightView:checkbox]; // Setup Tag so the textfield can be identified [textField setTag:-1]; [textField setDelegate:weakSelf]; // Setup textfield [textField setText:@"Essential"]; // Could be place holder text }]; [self presentViewController:alertWithText animated:YES completion:nil]; 

如果您纯粹要将该行作为勾号,则需要停止编辑该文本字段。

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ if(textField.tag == -1){ return NO; } return YES; } 

而你的button的动作

 -(void)buttonPressed:(UIButton*)sender { if(sender.selected){ [sender setSelected:FALSE]; } else { [sender setSelected:TRUE]; } } 

这里也有一些checkbox图片(有很多,你甚至可以做一个开关,尝试和animation)。

未选中的框 选中的框