Get Button Pressed Id on Swift Via Sender

How to identify a specific button as a sender

You'd have to add an IBOutlet for each of the buttons you want to compare in your code and then link the outlet to the actual button in your storyboard. Then you can check if the sender is that particular button.

If this does not make sense, please let me know and I'll elaborate further.

Swift - determine which button was pressed with switch

Try this,

1) Assign tag for each button

Button1.tag=1
Button2.tag=2
Button3.tag=3
Button4.tag=4

2) Then check your common button action

func buttonClicked(sender: UIButton)
{
switch sender.tag {
case 1: self.sender.hidden = true //button1
break;
case 2: self.sender.hidden = true //button2
break;
case 3: self.sender.hidden = true //button3
break;
case 4: self.sender.hidden = true //button4
break;
default: ()
break;
}

}

Highlighting the button with sender tag

You can create property with tags:

@property (nonatomic, strong) NSArray *tags;

Somewhere (for example, in viewDidLoad) initialize it with values used in storyboard:

tags = @[@1, @2, @3, @4, @5]

And select buttons using this tags

- (IBAction)buttonPressed:(UIButton *)sender {
for (int i = 0; i < tags.count; i++) {
UIButton *button = [self.view viewWithTag:tags[i]];
button.selected = (button.tag == sender.tag);
}
}

Or you can create IBOutlets for every 7 buttons and create array for it.

array = @[outlet1, ..., outlet7]

And select buttons using outlets

- (IBAction)buttonPressed:(UIButton *)sender {
for (int i = 0; i < array.count; i++) {
UIButton *button = array[i];
button.selected = (button.tag == sender.tag);
}
}

Missing Identifier for Radio Button when trying to identify the Sender

button.accessibilityIdentifier is the Accessibility Identity Identifier. The Identity Identifier is button.identifier.

@IBAction func text_radio_changed(_ sender: Any) {
let button:NSButton = sender as! NSButton
let id:String = button.identifier!.rawValue
print("===========>"+id)
}

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

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
}

How get product code when pressed button on uicollectionViewCell?

you can use with tag concept for identify which object you are pressed,

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
........

let productCode = productsObjectArray[indexPath.item].code
cell.favoriteBtn1.tag = indexPath.item
cell.favoriteBtn1.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside)
}

and your button action

@objc func didTapButton(_ sender: UIButton) {
let productCode = productsObjectArray[sender.tag].code
print("clicked product id: ", productCode)
}

How to get a button sender id? in SwiftUI

You can do it directly , without any needs of tag like that :

Button("Button title 1") {
print("Button tapped!")
messageMe(buttonId :"1")
}
.frame(minWidth:0, maxWidth: 300)
.padding()
.foregroundColor(.white)
.background(Color.gray)
.cornerRadius(20)

func messageMe(buttonId : String) {
// use the buttonId
}


Related Topics



Leave a reply



Submit