Swift - How to present ViewController when tapping button in a custom AlertController

后端 未结 4 421
失恋的感觉
失恋的感觉 2021-01-28 03:31

I am developing in Swift 2.3

I have an Utils class enabling me to create UIAlertController easily.

public class Utils {

    cl         


        
4条回答
  •  失恋的感觉
    2021-01-28 03:47

    To be more Specific and use that method in any class of project. For this make a function in NSObject class. Like:

    open  class func showAlert(_ delegate: UIViewController, message: String ,strtitle: String, handler:((UIAlertAction) -> Void)! = nil)
        {
            let alert = UIAlertController(title: strtitle, message: message, preferredStyle: UIAlertControllerStyle.alert)
    
            if handler == nil{
                alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
    
            }
            else
            {
                alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: handler))
            }
    
            delegate.present(alert, animated: true, completion: nil)
        }
    

    In Controller I will call the method and do the required work like:

     Alert.showAlert(self, message: "Message", strtitle: "Tittle!!", handler: {
                            (action : UIAlertAction) in
                      //Do your Work here
                    })
    

    Note: Here Alert is name of NSObject class.

提交回复
热议问题