jin
jin

Reputation: 57

a lot of button add same addTarget(_:action:for:) method

I'm working on a calculate app. and has ton of button which I store in a big stackView (created in SB and no outlet). each button cast some shadow (also set in SB attribute). I want to get rid of shadow when button was pressed. either tapGestureRecognizer or target action could only effect one UIButton. any convenience way to acheive

PS I mean when button .touchupinside or tapGestureRecognizer .end .start when finger move button should still cast the shadow

help appreciated

Upvotes: 0

Views: 306

Answers (3)

iPatel
iPatel

Reputation: 47049

There are so many way to achieve the goal but I'm going with below.

Just give my logic. Take one temporary optional variable of UIButton

Ex.

var myTemButton: UIButton()?

In the button action method

@IBAction func myButtonActionFunc(_ sender: UIButton) {
  if myTemButton == nil {
    myTemButton = sender 
  }
  else {
    // Use "myTemButton" and write here code for remove shadow Or other stuff that you want.
  }

  // use "sender" and write code here for apply shadow of button or other stuff that you want.
}

Upvotes: 0

Azhar Chara
Azhar Chara

Reputation: 86

UIButton will highlighted on click, so check button setting Change the title color in highlight state config to same as default state Or you can set:

[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];

If you want to control Highlighted by code, you can disable normal highlighted by subclass Button and disable in touchesBegin:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    if (self.state == UIControlStateHighlighted) {
        [self setHighlighted:NO];
    }
}

Upvotes: 3

crom87
crom87

Reputation: 1361

You can assign the same IBAction to multiple buttons, so that the same method is called for all of them

func buttonDidTouch(sender:UIButton) {}

If now you want to identify which exactly button is being called, you can use the UIButton.tag property to identify it. The tag can be set in the SB for each button.

Upvotes: 0

Related Questions