【问题标题】:How to get property of tapped button mapped from array?如何获取从数组映射的点击按钮的属性?
【发布时间】:2020-10-05 03:23:45
【问题描述】:

点击该按钮时,我试图从包含类属性的数组中打印属性,我已经阅读了一些答案,但没有一个有效。显然 println() 用于工作,但它似乎已被贬值。以下是有问题的代码:

这是按钮类

class Buttons{
   var nature = UIButton(type: UIButton.ButtonType.custom)
    var name: String
  

    init ( name: String) {
        self.name = name
       
    }
}

这是主类中的数组

var buttonsArray=[
    Buttons(name: "below")
            ,
    Buttons(name: "front")
           ,
    Buttons(name: "right")
           
    ]

映射:

for val in buttonsArray{

            val.nature.frame = CGRect(x: 2, y: 10, width: 20, height:20)
            val.nature.layer.cornerRadius = 0.5 * 0.12*bounds.size.width
            val.nature.clipsToBounds = true
            val.nature.backgroundColor = .blue
            sceneView.addSubview(val.nature)
        
        
            val.nature.adjustsImageWhenHighlighted = false
            val.nature.addTarget(self, action: #selector(profileButtonClicked), for: UIControl.Event.touchDown)
            val.nature.addTarget(self, action: #selector(profileButtonOut), for: UIControl.Event.touchUpInside)
            val.nature.addTarget(self, action: #selector(profileButtonOut), for: UIControl.Event.touchDragExit)
        
        }

分配函数

@objc func profileButtonClicked(button: self){
print(button.name)
}

我在功能想法中遇到错误,但重点是打印点击的按钮名称。 感谢您的宝贵时间

【问题讨论】:

    标签: swift events uibutton


    【解决方案1】:

    您可以更改函数参数,例如 @objc func profileButtonClicked(button: Button){ print(button.name) }

    供参考,你可以看看 [1]:What is "self" used for in Swift?

    您还提到您遇到了错误。如果你分享错误信息会更好。

    【讨论】:

    • 我已经试过了,它不起作用,并且有一个错误说:方法无法标记@objc,因为在Objective-C中无法表示参数的类型
    • 如果你想使用 Objective-c 中的类。那么你应该在你的 Button 类中至少扩展 NSObject。
    • 刚刚尝试了你的建议,我得到了这个错误:线程 1: EXC_BAD_ACCESS (code=2, address=0x7fff86e8d840)
    【解决方案2】:

    因为您的访问权限不正确。在那种情况下,我有两个解决方案给你,我都测试过并且效果很好。

    第一个,你需要修改你的 profileButtonClicked() 函数,如下所示

    @objc func profileButtonClicked(_ button: Buttons){
        for x in buttonsArray {
            if x.nature == button {
                print(x.name)
                break
            }
        }
    }
    

    或者我更喜欢这个,

    主类更改将是,

     var buttonList = [ 
        ButtonTest(name: "below"),
        ButtonTest(name: "front"),
        ButtonTest(name: "right")]
    
     for button in buttonList {
           sceneView.addSubview(button)
     }
    

    您可以将 Button 类替换为 ButtonTest,

    class ButtonTest: UIButton {
       var name: String
       init(name: String){
        self.name = name
        super.init(frame:.zero)
    
        self.frame = CGRect(x: 2, y: 10, width: 100, height:40)
        self.layer.cornerRadius = 0.5 * 0.12 * 200
        self.clipsToBounds = true
        self.backgroundColor = .blue
        //self.setTitle(name, for: .normal)
        self.adjustsImageWhenHighlighted = false
        //self.contentEdgeInsets  = UIEdgeInsets(top: 10, left: 10, bottom: 0, right: -25.0)
        //view.addSubview(val.nature)
        self.addTarget(self, action: #selector(profileButtonClicked), for: UIControl.Event.touchDown)
        //
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func profileButtonClicked(button: ButtonTest){
        print(button.name)
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2018-06-12
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 2019-11-09
      • 2018-11-28
      • 1970-01-01
      相关资源
      最近更新 更多