简体   繁体   English

很多按钮添加相同的addTarget(_:action:for :)方法

[英]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). 并有很多按钮,我将它们存储在一个大的stackView中(在SB中创建,没有插座)。 each button cast some shadow (also set in SB attribute). 每个按钮都会投射一些阴影(也在SB属性中设置)。 I want to get rid of shadow when button was pressed. 我想摆脱按下按钮时的阴影。 either tapGestureRecognizer or target action could only effect one UIButton. tapGestureRecognizer或目标动作只能影响一个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 PS我的意思是当按钮.touchupinside或tapGestureRecognizer .end .start。当手指移动按钮仍应投射阴影时

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: UIButton会在单击时突出显示,因此请选中按钮设置将突出显示状态配置中的标题颜色更改为默认状态,或者您可以设置:

[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: 如果要控制“代码突出显示”,则可以禁用子类Button突出显示的普通文本,并在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 您可以将相同的IBAction分配给多个按钮,以便为所有按钮调用相同的方法

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. 现在,如果您想确定正在调用的确切按钮,则可以使用UIButton.tag属性进行标识。 The tag can be set in the SB for each button. 可以在SB中为每个按钮设置标签。

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 取一个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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .addTarget方法将操作添加到UITextView - .addTarget method to add an action to a UITextView Swift 5无法向自定义按钮添加操作(AVPlayerViewController中的addTarget问题) - Swift 5 unable to add action to custom button(addTarget issues in AVPlayerViewController) 如何在 UICollectionView 中为按钮添加 addTarget? - How to add addTarget for Button in UICollectionView? 从xib addTarget加载的按钮未执行操作 - Button loaded from xib addTarget not performing action 在集合视图的笔尖内将addTarget更改为按钮动作 - addTarget to button action within nib of collection view 使用 addTarget:action:forControlEvents: 方法更改 UIImage? - Use addTarget:action:forControlEvents: method to change a UIImage? 在iOS上,有没有办法将方法addTarget添加到UIImageView? - On iOS, is there a way to add the method addTarget to UIImageView? UIButton AddTarget多次在同一个目标操作上只调用一次? - UIButton AddTarget multiple times on same target action only calls once? UIButton的替代选项addTarget:action:forControlEvents:IOS中的方法 - Alternative option of UIButton addTarget:action:forControlEvents: method in IOS 目标C:如何使用addTarget:action:forControlEvents:方法? - Objective C: How to use addTarget:action:forControlEvents: method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM