Home > Software design >  how to pass function in parameter using closure in swift 5
how to pass function in parameter using closure in swift 5

Time:09-27

I am new in swift. I have made generic actionsheet

import Foundation
extension UIAlertController{
    func action(mes:String,tit:String,tit2:String,operation1:(),operation2:()) {
        
        let actionSheet = UIAlertController(title: "", message:mes, preferredStyle: .actionSheet)
        
        let EButton = UIAlertAction(title:tit ,
                                              style: .default,
                                              handler: { _ in
                                                operation1
 
                                                })
        
        let AllEButton = UIAlertAction(title:tit2,
                                                  style: .default ,
                                                  handler:{ _ in
                                                    operation2
                                
                                                    })
        
        let cancelAction = UIAlertAction(title: "Cancel",
                                         style: .cancel,
                                         handler: nil)

        
        [EButton, AllEButton, cancelAction].forEach { $0.setValue(UIColor.red, forKey: "titleTextColor")
        }
        
        
        actionSheet.addAction(EButton)
        actionSheet.addAction(AllEButton)
        actionSheet.addAction(cancelAction)
        
        present(actionSheet, animated: true, completion: nil)
    }
}

I want to want to call this extension from viewControllerA

let actionView = UIAlertController()

    class viewControllerA: UIViewController {}
   
 private extension viewControllerA {
    func alertBottomSheat()  {
    actionView.action(mes: "Update",tit: "Update only",tit2: "Update All", operation1:saveEvent(),operation2:saveEvent())
    }
 @IBAction func deleteEventButtonClicked(_ sender: Any) {
actionView.action(mes: "delete ",tit: "Delete only",tit2: "Delete All ",operation1:deleteEvent(),operation2:deleteEvent(deleteAll: true))

}
}

Q1- I this right way to call the extension from viewControllerA extension?

Q2- please tell me how to pass function in action function parameter using closure in this line?

actionView.action(mes: "delete ",tit: "Delete only",tit2: "Delete All ",operation1:deleteEvent(),operation2:deleteEvent(deleteAll: true))

and how to use closure in handler of action sheet in this line

let EButton = UIAlertAction(title:tit ,
                                          style: .default,
                                          handler: { _ in
                                            operation1

                                            })

CodePudding user response:

You first need to create an extension for UIViewController instead of UIAlertController

Also, set the correct closure argument for the function and then call the function like this.

extension UIViewController {
    
    func action(message: String, firstTitle: String, secondTitle: String, firstAction: (() -> Void)? = nil, secondAction: (() -> Void)? = nil) {
        
        let actionSheet = UIAlertController(title: "", message: message, preferredStyle: .actionSheet)
        
        let eButton = UIAlertAction(title: firstTitle ,
                                    style: .default,
                                    handler: {_ in firstAction?()})
        
        let allEButton = UIAlertAction(title: secondTitle,
                                       style: .default ,
                                       handler: {_ in secondAction?()})
        
        let cancelAction = UIAlertAction(title: "Cancel",
                                         style: .cancel,
                                         handler: nil)
        
        
        [eButton, allEButton, cancelAction].forEach { $0.setValue(UIColor.red, forKey: "titleTextColor")}
        
        
        actionSheet.addAction(eButton)
        actionSheet.addAction(allEButton)
        actionSheet.addAction(cancelAction)
        
        present(actionSheet, animated: true, completion: nil)
    }
}

Usage

private extension viewControllerA {
    func alertBottomSheat()  {
        self.action(message: "Update", firstTitle: "Update only", secondTitle: "Update All", firstAction: saveEvent, secondAction: saveEvent)
    }
    
    @IBAction func deleteEventButtonClicked(_ sender: Any) {
        self.action(message: "delete ", firstTitle: "Delete only", secondTitle: "Delete All ", firstAction: { self.deleteEvent()}, secondAction: { self.deleteEvent(deleteAll: true) })
        
    }
    
    func saveEvent() {
        
    }
    
    func deleteEvent(deleteAll: Bool = false) {
        
    }
}

Note: Fixed coding standard rule and var names.

  • Related