How to add alert message to a global class

后端 未结 5 1237
刺人心
刺人心 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 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)
    

提交回复
热议问题