UIAlertController
Some time we need to use UIAlertcontroller to display some message , for that we need to define in each class where we want to use but we can make a common class from where we can access from any controller and display.
Follow some steps to do that.
1: create a class like GlobalData.swift and its parent would be NSObject
2: write the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
func getLocationWindow(view:UIViewController){ let AC = UIAlertController(title: "Enter Your Location", message: "Select Location", preferredStyle: .alert) AC.addTextField { (textField) in textField.placeholder = "Enter Location"; //textField.addTarget(self, action: #selector(self.myTarget), for: UIControlEvents.allEvents) textField.delegate = self; textField.tag = 999 textField.isUserInteractionEnabled = true } let addBtn = UIAlertAction(title: "ADD LOCATION", style:.default, handler: {(_ action: UIAlertAction) -> Void in let okBtn = UIAlertAction(title: "GO TO LOCATION", style: .default, handler: {(_ action: UIAlertAction) -> Void in }) let noBtn = UIAlertAction(title: "CANCEL", style:.destructive, handler: {(_ action: UIAlertAction) -> Void in }) AC.addAction(addBtn) AC.addAction(okBtn) AC.addAction(noBtn) view.parent!.present(AC, animated: true, completion: { _ in }) } |
3: Now from any class whose parent is UIViewController
call like as:
globalObjectHome.getLocationWindow(view:self)
Note: globalObjectHome is object of GlobalData.swift class.