Can you try below code:
let alert = UIAlertController(title: "Test alert title", message: "Test alert body", preferredStyle: .Alert)
alert.addAction(callback())
presentViewController(alert, animated: true, completion: nil)
with callback
:
func callback() -> UIAlertAction {
return UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
print("alert action")
})
}
I don't know exactly what you want. But i think you want to get a TextField
inside UIAlertController
. May be that below code will help you.
var alert: UIAlertController?
let tfTag = 123
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if alert == nil {
self.alert = UIAlertController(title: "Test alert title", message: "Test alert body", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
// Get your TextField
if let tf = self.alert!.view.viewWithTag(self.tfTag) as? UITextField {
print("Value: \(tf.text)")
}
})
self.alert!.addAction(action)
// Insert UITextField
let textField = UITextField()
textField.text = "Hello World"
textField.tag = tfTag
self.alert!.view.addSubview(textField)
presentViewController(self.alert!, animated: true, completion: nil)
}
}
Hope that helps!