【问题标题】:Is it possible to use an array entry in a #selector?是否可以在#selector 中使用数组条目?
【发布时间】:2020-04-22 03:40:06
【问题描述】:

我正在向 TextView 动态添加标签和按钮。这是通过循环遍历包含标签名称、按钮名称和按钮功能名称的数组来驱动的。为简洁起见,我已将此处的代码更改为使用硬编码索引而不是循环,并且我没有包含标签和按钮创建函数。标签创建得很好。第一个按钮也创建得很好,因为我将 Button1Clicked 指定为 #selector 参数。仅当我将 Button2Clicked 指定为 #selector 参数时,才能正确创建第二个按钮。如果我尝试在按钮 #selector 参数中引用数组,我会收到编译器错误“'#selector' 的参数不引用 '@objc' 方法、属性或初始化程序”。假设可以使用包含@objc 函数名称的数组作为#selector 的参数,有人可以为我提供适当的语法吗?我正在使用 Swift 5 和 Xcode 11.2。提前感谢您的帮助。

@objc func Button1Clicked(sender: UIButton) {
    print("You pressed button 1")
}

@objc func Button2Clicked(sender: UIButton) {
    print("You pressed button 2")
}
    .
    .
    .

    var labelNames = [UILabel]()
    let Label1:UILabel = UILabel()
    let Label2:UILabel = UILabel()
    labelNames = [Label1, Label2]

    var buttonNames = [UIButton]()
    let Button1:UIButton = UIButton()
    let Button2:UIButton = UIButton()
    buttonNames = [Button1, Button2]

    let buttonSelectorNames = ["Button1Clicked", "Button2Clicked"]

    configureLabel(labelNameIn: labelNames[0],
                   textIn: "aaaaa",
               textColorIn: appColorWhite,
                   backgroundColorIn: appColorBlue,
                   yPositionIn: 0)
    statusTextview.addSubview(labelNames[0])

    configureLabel(labelNameIn: labelNames[1],
                   textIn: "-- bbbbb",
                   textColorIn: appColorWhite,
               backgroundColorIn: appColorBlue,
                   yPositionIn: 25)
    statusTextview.addSubview(labelNames[1])

    configureButton(buttonNameIn: buttonNames[0],
                    xPositionIn: 0,
                    yPositionIn: 50,
                    textIn: "B1",
                    textColorIn: appColorBlack,
                    backgroundColorIn: appColorBrightGreen,
                    textViewIn: statusTextview)
    buttonNames[0].addTarget(self,
                             action: #selector(Button1Clicked),
                             for: .touchUpInside)
    statusTextview.addSubview(buttonNames[0])

    configureButton(buttonNameIn: buttonNames[1],
                    xPositionIn: 100,
                    yPositionIn: 50,
                    textIn: "B2",
                    textColorIn: appColorBlack,
                    backgroundColorIn: appColorBrightGreen,
                    textViewIn: statusTextview)
    buttonNames[1].addTarget(self,
                      action: #selector(buttonSelectorNames[1]),
                      for: .touchUpInside)
    statusTextview.addSubview(buttonNames[1])

【问题讨论】:

  • 问题在于buttonSelectorNames是一个字符串数组,而不是一个选择器数组。

标签: swift button selector


【解决方案1】:

你不需要为每个按钮定义按钮点击事件。你可以定义通用函数并将它分配给循环中的所有按钮。然后你可以通过按钮标签指定

在您的循环中将当前索引分配为按钮标签

buttonNames[currentIndex].tag = currentIndex
buttonNames[currentIndex].addTarget(self,
                             action: #selector(didButtonClicked(_ :)),
                             for: .touchUpInside)

按钮点击功能

@objc func didButtonClicked(_ sender : UIButton){
        switch sender.tag{
        case 0: //clicked button with tag 0
        case 1: //clicked button with tag 1
        default: break
        }

    }

【讨论】:

  • 感谢您的出色建议。它使我的代码更易于阅读。
【解决方案2】:

过去,我处理过相同类型的需求。我通过将selector 存储在Array 中来做到这一点。

请参考代码sn-ps。我相信它会对你有所帮助。

代码:

class ViewController: UIViewController {

    let selector = [#selector(Button1Clicked),#selector(Button2Clicked)]

    override func viewDidLoad() {
        super.viewDidLoad()
        let btn = UIButton(frame: self.view.bounds)
        self.view.addSubview(btn)
        btn.addTarget(self, action: selector[0], for: .touchUpInside)

    }

    @objc func Button1Clicked(){
        print("Button1Clicked")
    }

    @objc func Button2Clicked(){

    }
}

输出:

【讨论】:

  • 完美。正是我需要的。谢谢。
  • 很高兴听到!快乐编码
猜你喜欢
  • 2022-01-23
  • 2023-03-19
  • 2021-11-08
  • 2022-07-20
  • 2012-10-05
  • 1970-01-01
  • 2013-12-29
  • 2015-05-27
  • 2014-03-28
相关资源
最近更新 更多