How to add alert message to a global class

后端 未结 5 1238
刺人心
刺人心 2021-01-26 09:29

I have the following code repeated in many of my view controllers. I would like to refactor it into a global class/view controller. However, I still want it to be s

相关标签:
5条回答
  • 2021-01-26 09:59

    You can create a UIViewController extension and implement this method there. This will allow you to call it from any UIViewController subclass.

    extension UIViewController {
    
        func displayAlertMessage(title: String, message: String)
        {
            let alertMessage = UIAlertController(title: title, message: message, preferredStyle:UIAlertControllerStyle.alert);
    
            let okAction = UIAlertAction(title:"OK", style: .default, handler:nil);
    
            alertMessage.addAction(okAction);
    
            self.present(alertMessage, animated: true, completion: nil);
        }
    }
    

    You could also add a handler and completion parameters to the displayAlertMessage function, which would allow you to customize the both the action handler and what happens when the alert finishes being presented, from the calling point.

    0 讨论(0)
  • 2021-01-26 10:12

    You can use SCLAlertView for simple alert creation. The alert is always shown from the current view controller and returns to this controller when the alert is closed. This static function shows a simple message with an OK button. Of course, you can also add text fields, buttons and other elements. These alerts are especially nice because you can call them at any point in your code, even from background queues.

    class Alerts {
    
        static func showAlert(title: String, message: String) {
            DispatchQueue.main.async {
                let alert = SCLAlertView()
                alert.showCustom(title, subTitle: message, color: UIColor.blue, icon: UIImage(named: "logo")!, closeButtonTitle: "OK")
            }
        }
    
    }
    
    0 讨论(0)
  • 2021-01-26 10:18

    Swift 4.0

    You can use action closure for multiple actions.

    extension UIViewController {
    
        func popupAlert(title: String?, message: String?, actionTitles:[String?], actions:[((UIAlertAction) -> Void)?]) {
            let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
            for (index, title) in actionTitles.enumerated() {
                let action = UIAlertAction(title: title, style: .default, handler: actions[index])
                alert.addAction(action)
            }
            self.present(alert, animated: true, completion: nil)
        }
    }
    

    And you can use like this in UIViewController

     popupAlert(title: kTitle, message: "This is test alert!!!!" , actionTitles: ["Ok","Cancel"], actions: [ { action1 in
                //perform action for OK button
                }, { action2 in
               //perform action for cancel button
      }])
    
    0 讨论(0)
  • 2021-01-26 10:20

    I like extensions for this.

    If you extend uiviewcontroller you should be able to put this function inside and then you'd be able to call it from any view controller.

    0 讨论(0)
  • 2021-01-26 10:21

    Its just simple Just create a new NSObject class and Do the following

    class WrapperClass: NSObject 
    {    
        class func BasicAlert(_ title : String, message : String, view:UIViewController)
        {
            let alert = UIAlertController(title:title, message:  message, preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            view.present(alert, animated: true, completion: nil)
        }
    }
    

    Usage

    WrapperClass.BasicAlert("Error", message: "Bluetooth Headset is Not Connected, Please Retry", view: self)
    
    0 讨论(0)
提交回复
热议问题