How To Use One IBAction For Multiple Buttons In Swift

We have learnt how to add button click event process function in article iOS Add Click Event To UIButton Swift Example. In that article, if you want to process multiple button’s click event, you should create multiple IBAction function to process. But sometimes, for code simplicity, you need to use only one IBAction function to process multiple button’s click event, this article will tell you how to do it.

1. Use One IBAction Function To Process Multiple Button’s Click Event Demo.

First let us look at the example demo gif picture as below. There are one label and two button in the screen, when you click different button, it will display different text in the label. The two button’s click event are processed in one IBAction function.

use one ibaction for multiple swift buttons example

2. Create Example Project Steps.

  1. Create a new swift Xcode project, you can refer article How To Create A Swift Project In Xcode.
  2. Open Xcode UI component Library and add one label and two buttons in the screen by edit Main.storyboard file. You can refer article How To Display Xcode Library, Assistant Editor, Inspectors Window.
  3. To make the three UI component full fill the screen width, just select it, then click View —> Inspectors —> Show Size Inspector menu item at the Xcode top menu bar to display the Size Inspector. Then click Fill Container Horizontally drop down item to select it in the Size inspector¬† —>¬† View —> Arrange ( Position View ) —>Fill Container drop down list.
    xcode size inspector window
  4. To make the label text align center, you should select the label in the Main.storyboard file, then click View —> Inspectors —> Show Attributes Inspector menu item at the Xcode top menu bar to open the attributes inspector, then click Center alignment in Alignment section.
    xcode attributes inspector change alignment
  5. Now we need to set tag attribute value for the two buttons. Click Main.storyboard file to open it, then select Button One in the screen, and click View —> Inspectors —> Show Attributes Inspector menu item to display the button’s attribute inspector panel in Xcode project right side like below.
    xcode attributes inspector set tag attribute value
  6. Scroll down the attributes inspector panel, until you see Tag attribute in the View section like above, then input value 1 in the text box after Tag attribute. The tag value for each UI component should be unique in same Xcode project, we use this tag value to identify the ui component uniquely.
  7. Select Button Two and set it’s tag attribute value with 2.
  8. Now each button has one Tag attribute value, and the value is unique.
  9. Now open the assistant editor by click View —> Assistant Editor —> Show Assistant Editor menu item at Xcode top menu bar. Press Control key and drag Button One from Main.storyboard to the assistant editor to create one IBAction function to process it’s Touch Up Inside event. You can refer article How To Connect Storyboard UI Component To ViewController Class Code.
  10. To make Button Two use same IBAction function to process it’s Touch Up Inside event, you should press Control key and drag Button Two from Main.storyboard to the assistant editor and connect it to the same IBAction function which Button One is used like in below gif picture. So to connect multiple buttons to same IBAction function, you just need to press Control key and drag every button from Main.storyboard to same IBAction function in assistant editor.
    ios swift xcode connect the second button to the same ibaction function
  11. Now you only need to add below swift source code in ViewController.swift source file, in the source code, we use event source button’s tag attribute value to identify which button sends the event, then change the label text accordingly.
    //
    //  ViewController.swift
    //  SwiftOneFunctionPorecessMultipleButtonClickExample
    //
    //  Created by song zhao on 7/9/19.
    //  Copyright © 2019 dev2qa.com. All rights reserved.
    //
    
    import UIKit
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var msgLabel: UILabel!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            msgLabel.text = ""
        }
    
    
        @IBAction func btnClick(_ sender: UIButton) {
            
            switch sender.tag{
                
            case 1:
                msgLabel.text = "Button one is clicked."
            case 2:
                msgLabel.text = "Button two is clicked."
            default:
                msgLabel.text = ""
            }
        }
    }
    
    
READ :   How To Create Multiple Button And Add Button Action Function Programmatically In iOS Swift

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.