简体   繁体   中英

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

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];
    }
}

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.

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.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM