Search code examples
iosiphonexcodeswift5xcode12

how to get the clicked element details like textvalue or button name or id in Xcode ios application


I'm trying to get the user interaction in iOS application, For example where user clicked, which button user clicked etc. is there is any way to capture these details in Xcode swift?


Solution

  • You may do that like this:

    button.addTarget(self, action: #selector(onButtonClick(_:)), for: .touchUpInside)
    
    @objc
    func onButtonClick(_ sender: UIButton) {
        // you may get the detail by 'sender' object here
        sender.isSelected = !sender.isSelected
    }
    

    If you have multiply sender that invoke the same method, you can give them a tag for each. then you can detect which view invoke the event.

    if sender.tag == YOUR_TAG {
        // do sth.
    } else {
        // do sth. else
    }