skip to Main Content

I’m studying Swift and I have a question.
When we make some button’s Action function, sometimes tags are used.
After all to set button’s tags with code, we must connect button’s Outlet from storyboard.
Why use tags instead of outlet’s variable name?

3

Answers


  1. Any number of buttons can have a single action and we then need tag to distinguish action based on button tag. you don’t actually need outlet for each button if you are setting tag from storyboard, Here is a detailed articles about tags:

    Working With Multiple UIButtons and Utilizing Their Tag Property

    Login or Signup to reply.
  2. Many cases many button have the same ibaction. In this situation , tag can help

    Login or Signup to reply.
  3. As others have said the function could be completely independent of the button, it could be in another class altogether.

    Buttons may not be in StoryBoards and could be created programatically, potentially dynamically, so there maybe no case of checking if the button passed to the function is the same as a local variable (as a button may not be an ivar on the class the function is based on)

    Tags give an easy way to provide some identifier to button or view that can be cross referenced when the function is called in order to achieve the desired out come

    Here’s an example to illustrate, bear in mind this is somewhat contrived in order to provide an detail

    In you ViewController you need to dynamically create a button but don’t want to store it as a variable on the VC. You also have a separate class which is handling taps

    We could create an enum to handle button types, this handles the tag and title for the button type

    enum ButtonType: String {
        case nonIvar
        case other
        
        var tag: Int  {
            switch self {
            case .nonIvar:
                return 0
            case .other:
                return 1
            }
        }
        
        var title: String {
            switch self {
            case .nonIvar:
                return "Non iVar Button"
            case .other:
                return "Some other Button"
            }
        }
    }
    

    we then have our class that handles the tap function

    class ButtonHandler {
        @IBAction func handleTap(_ sender: UIButton) {
            if sender.tag == ButtonType.nonIvar.tag {
                // do something with the none ivar buton
            } else {
                // handle other type
            }
        }
    }
    

    then in our view controller we create our button hooking the two instances above up

    let handler = ButtonHandler()
    func addMyButton() {
    let myButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
    let buttonType: ButtonType = .nonIvar
    myButton.setTitle(buttonType.title, for: .normal)
    myButton.tag = buttonType.tag
    myButton.addTarget(self, action: #selector(handler.handleTap(_:)), for: .touchUpInside)
    view.addSubview(myButton)
    }
    

    This means that the handler can be used on any class and the button types could be equally used throughout the app and the behaviour would be the same without duplicating code

    Hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search