是否可以在#selector中使用数组条目?

问题描述 投票:0回答:2

我正在向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])
swift button selector
2个回答
0
投票

您无需为每个按钮定义按钮单击事件。您可以定义通用功能并将其分配给循环中的所有按钮。然后您可以通过按钮标记进行指定

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

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
        }

    }

0
投票

过去,我处理相同类型的要求。我是通过将selector存储在Array中来实现的。

请参阅代码段。我相信它将为您提供帮助。

代码:

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(){

    }
}

输出:

Output

© www.soinside.com 2019 - 2024. All rights reserved.