How To Use One Function To Process Multiple Button Click Event Programmatically In Swift

We have learnt how to use one IBAction function to process multiple button click event in article How To Use One IBAction For Multiple Buttons In Swift. In that example, we set a tag attribute value for each swift button, and then use that tag attribute value to distinguish which button is clicked in one IBAction function. This article will show you an example about how to process multiple button click event in one function programmatically.

1. Process Multiple Button Click Event In One Swift Function Programmatically Example.

First let us look at the example demo gif picture as below. There are two buttons in the screen, when click each button, it will change the button title text color and background color reversely.

response multiple button click event in one swift function example

2. Example Source Code.

Below is the example source code, we only need to edit ViewController.swift file. Here are some tips that you need to notice.

  1. Invoke UIButton’s addTarget¬†function to connect button click event to related event process function.
    btn1.addTarget(self, action: #selector(processButtonClickEvent), for: UIControl.Event.touchDown)
  2. The button event process function should start with @objc.
  3. Because we want to use one event function to process two button’s click event, so we need to set a unique tag attribute value for each button to distinguish which button is clicked in the function.

ViewController.swift

//
//  ViewController.swift
//  SwiftProcessMultipleButtonClickEventInOneFunctionProgrammaticallyExample
//
//  Created by song zhao on 7/13/19.
//  Copyright © 2019 dev2qa.com. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    // Get screen width and height value.
    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    
    // Tag value is an integer number which will be set to UIButton's tag property. It is used to identify UIButton uniqualy.
    let TAG_1 = 1
    let TAG_2 = 2
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Create button one.
        let btn1:UIButton = createButton(title: "Button One", titleColor: UIColor.red, backgroundColor: UIColor.green)
        
        // Move button one's center to half screen width and three quarters of screen height.
        btn1.center = CGPoint(x: screenWidth/2, y: 3*screenHeight/4)
        
        // Set button one's tag property value.
        btn1.tag = TAG_1
        
        // Add button click event process method to this button, when you click this button, the processButtonClickEvent method will be invoked.
        btn1.addTarget(self, action: #selector(processButtonClickEvent), for: UIControl.Event.touchDown)
        
        // Add btn1 to main screen.
        self.view.addSubview(btn1)
        
        
        
        // Create button two.
        let btn2:UIButton = createButton(title: "Button Two", titleColor: UIColor.yellow, backgroundColor: UIColor.blue)
        
        // Move button two's center to half screen width and half screen height.
        btn2.center = CGPoint(x: screenWidth/2, y: screenHeight/2)
        
        // Set button two's tag property value.
        btn2.tag = TAG_2
        
        // Add button click event process function for btn2.
        btn2.addTarget(self, action: #selector(processButtonClickEvent), for: UIControl.Event.touchDown)
        
        // Add btn2 to main screen.
        self.view.addSubview(btn2)
        
    }

    /* This function is used to process multiple button click event, the source button object is distinguished by the button's tag attribute's value. */
    @objc func processButtonClickEvent(srcObj : UIButton) -> Void{
        
        // If button one is clicked.
        if(srcObj.tag == TAG_1){
            
            if(srcObj.backgroundColor == UIColor.red){
                
                srcObj.backgroundColor = UIColor.green
                
                srcObj.setTitleColor(UIColor.red, for: UIControl.State.normal)
                
            }else if(srcObj.backgroundColor == UIColor.green){
                
                srcObj.backgroundColor = UIColor.red
                
                srcObj.setTitleColor(UIColor.green, for: UIControl.State.normal)
        
            }
            
        }
        // If button two is clicked.
        else if(srcObj.tag == TAG_2){
            
            if(srcObj.backgroundColor == UIColor.blue){
                
                srcObj.backgroundColor = UIColor.yellow
                
                srcObj.setTitleColor(UIColor.blue, for: UIControl.State.normal)
                
            }else if(srcObj.backgroundColor == UIColor.yellow){
                
                srcObj.backgroundColor = UIColor.blue
                
                srcObj.setTitleColor(UIColor.yellow, for: UIControl.State.normal)
                
            }
        }
    }
    
    /* This function create and return a UIButton object with provided title, title color and background color. */
    func createButton(title:String, titleColor:UIColor, backgroundColor:UIColor) -> UIButton{
        
        // Create button one as system default type.
        let btn:UIButton = UIButton(type: UIButton.ButtonType.system)
        
        // Set button one's size and location.
        btn.frame = CGRect(x: 10, y: 10, width: screenWidth/2, height: 100)
        
        // Set button one's title.
        btn.setTitle(title, for: UIControl.State.normal)
        
        // Set button ont's title color.
        btn.setTitleColor(titleColor, for: UIControl.State.normal)
        
        // Set button one's background color to green.
        btn.backgroundColor = backgroundColor
        
        // Set button one's text font.
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 30)
        
        return btn
    }
}

READ :   How To Use Swift String Example

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.