'Which button was pressed in a custom swift cell

I am using three separate SWIFT buttons in custom cell in a tableView. Each button is a counter and therefore, I increment the label/text of the button when it is pressed. What I need to do is increment the appropriate button when the user presses it but with the text label constantly changing, I need a what to determine which button was pressed other than by using the sender.titleLabel! function.

Can you please help me (by example code) determine how I can find out which of the three buttons were pressed. Perhaps using the name in the "Referencing Outlets"



Solution 1:[1]

I am to put some constraint in place here to answer your question better.

From the post, it seems that you are using storyboard and prototype cell within a UITableView to create the buttons and text labels.

Within the Storyboard, if you click on the individual buttons, you will be able to set a tag to it (its on the right navigation under the inspector).

You need to set different tag for different buttons. Eg. Button A - tag 104 and like wise.

Next in your codes add the following line.

[(UIButton *)[cell.contentView viewWithTag:104] addTarget:self action:@selector(selectButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

Note the tag being used and also the selector is "selectButtonPressed"

You will then need to create a method or function for selectButtonPressed.

Cheers.

Solution 2:[2]

Use sender.self like in example below:

if zeroPcButton == sender.self {
    zeroPcButton.isSelected = true
    tenPcButton.isSelected = false
    twentyPcButton.isSelected = false
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lancegoh
Solution 2 Tyler2P