A Protocol definds a set of methods, properties and reqirements that would be implemented by the class, struck or enum which conforms to that protocol. iOS even uses protocol with the delegate design pattern. You've might have notice it using UITableViewDelegate or UITableViewDataSource.

Today we will create our own custom alert view, and use a custom protocol to handle the button callbacks in our view controller.

First we will create our alert view (AlertView) and decare our protocol (AlertControllerDelegate). Then we will declare a weak reference to our protocol inside of our alert view class. 

Next we will decare a buildAlertView function that take a param of type AlertControllerDelegate and set our self delegate with the delegate param that was passed in. Tnen we will add the views that we want to show in our alert including our confirm and decline buttons, And when they are clicked we will invoke our protocol functions updating the listening view controller.

The below code is what we should get.
//My Protocol decleration
protocol AlertControllerDelegate {
    func didConfirm()
    func didDecline()
}
class AlertView : UIView {
    weak var delegate : AlertControllerDelegate?
    
    override init(frame: CGRect) {
        self.frame = frame
    }
    
    func buildAlertView(delegate : AlertControllerDelegate){
        
        //Set our delegate
        self.delegate = delegate
    
        let alertLabel = UILabel(frame : CGRect(
                x : 10, 
                y : 0, 
                width : self.frame.width - 20, 
                height : 30))
                
        alertLabel.text = "Do you want to confirm or decline"
        
        let confirmBtn = UIButton(frame : CGRect(
                x : 10, 
                y : alertLabel.frame.origin.y + alertLabel.frame.height + 10 , 
                width : self.frame.width - 20, 
                height : 50))
                
        confirmBtn.setTitle(title : "Confirm", forState : .normal)
        confirmBtn.addTarget(self, action: #selector(confirmUserAction), for: .touchUpInside)
        
        let declineBtn = UIButton(frame : CGRect(
                x : 10, 
                y : confirmBtn.frame.origin.y + confirmBtn.frame.height + 10 , 
                width : self.frame.width - 20, 
                height : 50))
                
        declineBtn.setTitle(title : "Decline", forState : .normal)
        declineBtn.addTarget(self, action: #selector(declineUserAction), for: .touchUpInside)
        
        self.addSubView(alertLabel)
        self.addSubView(confirmBtn)
        self.addSubView(declineBtn)
    }
    
    @objc func confirmUserAction(){
        //Notify the listener that the confirm action was clicked.
        delegate?.didConfirm()
    }
    
    @objc func declineUserAction(){
        //Notify the listener that the decline action was clicked.
        delegate?.didDecline()
    }
}

Now that we have our alert view with our protocol declared, We will make use of it in our view controller.

First we will create and display a button, And when clicking on that button we will create display our alert. Next we need to conform to our protocol by implementing the didConfirm and didDecline functions we've decared earlier.

It should look somthing like this.

class MyViewController : UIViewController, AlertControllerDelegate {

    func viewDidLoad(){
        //My view did load code
    }
    
    func viewWillAppear(_ animated: Bool){
        //My view will appear code
        
        let showAlertBtn = UIButton(frame : CGRect(
                x : UIScreen.main.frame.width * 0.2, 
                y : UIScreen.main.frame.width * 0.5 - 25, 
                width : UIScreen.main.frame.width * 0.6, 
                height : 50))
                
        showAlertBtn.setTitle(title : "Confirm", forState : .normal)
        showAlertBtn.addTarget(self, action: #selector(showAlertView), for: .touchUpInside)
        self.view.addSubView(showAlertBtn)
    }
    
    @objc func showAlertView(){
        //Create and show the alert
        let alertView =
AlertView(frame : CGRect(
                x : UIScreen.main.frame.width * 0.1, 
                y : UIScreen.main.frame.width * 0.25, 
                width : UIScreen.main.frame.width * 0.8, 
                height : UIScreen.main.frame.height * 0.5))
                
        alertView.buildAlertView(delegate : self)
        
        self.view.addSubView(alertView)
    }
    
    func didConfirm(){
        //Handle User Confirm action
    }
    
    func didDecline(){
        //Handle User Decline action
    } 
}

That it! If you want to learn more about swift protocols. you can find more in the Apple official docs.
Back to Top