I am creating an registration dialog in swift with 3 text field and one Switch and I successfully add three text field two the Alert. The following code shows the same.
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) {
// ...
}
Now I need to add a switch programmatically to the Alert View.We are doing this in Swift2. Is it possible?, i am new to Swift.
You can use the RightView of the TextField to add a button. Adding a switch would be nice but the switch does not fit into the TextField height nor can you change the height. To this end you can add a button and use images to make a TickBox.
I have ripped this out of a project so the example image is a little more than below.
In the ViewController header add the TextField Delegate
Then create your AlertController and add the TextField
You need to stop the textfield from editing if you purely want that line to be a tick.
And your action for your button
Here are some tick box images too (there are plenty out there, you could even make a switch and try and animate).
This answer is for Objective C. It doesn't use text fields but it does add a
UISwitch
to aUIAlertController
as asked in the main question. I didn't find anything on SO that does exactly this so I'm posting this answer here, rather than posting another question that will get dinged as a duplicate.This solution is used to enable users to sort a
UITableView
menu (of a list of projects...)Thanks to the answer by @technerd, I also made the
UISwitch
change the text of aUILabel
that is also on the sameUIAlertController
view. It uses KVC (Key-Value Coding) in the layer to pass theUILabel
id to the target action when theUISwitch
value is changed. (See thesetOrderLabelText
method in the code)I was also trying to get around the trick of adding newlines ("\n\n\n\n") to the title or message to artificially move things around, by using constraints.
I used a horizontal
UIStackView
to hold theUISwitch
and it's correspondingUILabel
, and then used constraints to set the top anchor of theUIStack
and a height constraint on theUIAlertController
view to make it big enough to contain theUIStackView
and the UIAlertController title.I don't think it is possible to get the height of the title of the
UIAlertController
or the height of the action buttons. So I came up with values that worked well on an iPhone X and an iPad 2. As in other SO answers, I will likely come up with a home grown (or find one on GitHub) solution to make this more robust. But since I got this far and got so much from other awesome SO answers, I wanted to give back a bit and share my results.Here's a screenshot:
And here's the code:
This may help you.
Add this method call
alertController.view.addSubview(createSwitch())
in above code afteralertController.addAction(OKAction)
.OutPut :