Alerts (popups)
Simple alert
DispatchQueue.main.async {
let alert = UIAlertController(title: "Hello", message: "This is an alert", preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true)
}
Two buttons
DispatchQueue.main.async {
let alertController = UIAlertController(title: "Simple", message: "Simple alertView demo with Cancel and Ok.", preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { (result : UIAlertAction) -> Void in
}
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
}
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
Showing it inside a tableviewcell, instead of self.present, use:
UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
With input field
let alertController = UIAlertController(title: "Grocery Item", message: "What to buy now?", preferredStyle: UIAlertControllerStyle.alert)
alertController.addTextField { (textField: UITextField) in
}
let addActionTwo = UIAlertAction(title: "ADD", style: UIAlertActionStyle.default) { (action: UIAlertAction) in
let itemString : String?
if(alertController.textFields?.first?.text != "")
{
itemString = alertController.textFields?.first?.text
}
else{return}
//what to after they key in data
self.groceries.append(itemString)
self.tableView.reloadData()
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: nil)
alertController.addAction(addActionTwo)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
If you want to make the input field one with a keypad
look for the method of addTextField with parameter configurationHandler
alert.addTextField(configurationHandler: { textField in
textField.keyboardType = .numberPad
})
DispatchQueue.main.async {
let alert = UIAlertController(title: "Hello", message: "This is an alert", preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true)
}
Two buttons
DispatchQueue.main.async {
let alertController = UIAlertController(title: "Simple", message: "Simple alertView demo with Cancel and Ok.", preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { (result : UIAlertAction) -> Void in
}
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
}
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
Showing it inside a tableviewcell, instead of self.present, use:
UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
With input field
let alertController = UIAlertController(title: "Grocery Item", message: "What to buy now?", preferredStyle: UIAlertControllerStyle.alert)
alertController.addTextField { (textField: UITextField) in
}
let addActionTwo = UIAlertAction(title: "ADD", style: UIAlertActionStyle.default) { (action: UIAlertAction) in
let itemString : String?
if(alertController.textFields?.first?.text != "")
{
itemString = alertController.textFields?.first?.text
}
else{return}
//what to after they key in data
self.groceries.append(itemString)
self.tableView.reloadData()
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: nil)
alertController.addAction(addActionTwo)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
If you want to make the input field one with a keypad
look for the method of addTextField with parameter configurationHandler
alert.addTextField(configurationHandler: { textField in
textField.keyboardType = .numberPad
})
Comments
Post a Comment