Swift-将以编程方式创建的按钮和标签彼此链接

时间:2018-11-27 22:38:53

标签: swift xcode uibutton tags programmatically-created

我正在以编程方式创建所有内容,并且在链接多个按钮以响应选定的自定义操作时遇到问题。

蓝色按钮是通过程序创建的,我使用标签来跟踪按下了哪个按钮。选择蓝色按钮后,将弹出一个动作菜单,可以通过按添加图标将动作链接到该按钮。

您可以通过单击“选择”来选择所需的操作,然后从“ o”按钮中拖动并拖动以创建连接器。

我将所选动作以及与之相连的按钮存储在字典中

问题在于,以编程方式创建它们时,如何将同一行中的“ o”按钮,“选择”按钮和“名称”标签彼此链接?我没有使用tableview创建动作。这样会更容易使用吗?

enter image description here

这将创建操作行

// MARK: - ACTION Input
func createAction()
{
    let actionTabContainer = UIView()
    actionTabContainer.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
    actionTabContainer.translatesAutoresizingMaskIntoConstraints = false
    actionTabContainer.backgroundColor = UIColor.darkGray
    actionTabContainer.layer.borderWidth = 2
    actionTabContainer.layer.borderColor = UIColor(red: 29/255.0, green: 30/255.0, blue: 33/255.0, alpha: 1.0).cgColor

    actionScrollViewContainer.addSubview(actionTabContainer)

    actionTabContainer.widthAnchor.constraint(equalToConstant: actionScrollViewContainer.frame.width).isActive = true
    actionTabContainer.heightAnchor.constraint(equalToConstant: 50).isActive = true
    actionTabContainer.leftAnchor.constraint(equalTo: actionScrollViewContainer.leftAnchor, constant: 10).isActive = true
    actionTabContainer.topAnchor.constraint(equalTo: actionScrollViewContainer.topAnchor, constant: 2 + constantAdd).isActive = true
    constantAdd = constantAdd + 50

    let connectorBtn = UIButton()
    connectorBtn.createRectangleButton(buttonPositionX: 0, buttonPositionY: 0, buttonWidth: 0, buttonHeight: 0, buttonTitle: "O", buttonTag: 400)
    connectorBtn.translatesAutoresizingMaskIntoConstraints = false
    connectorBtn.backgroundColor = UIColor.gray

    actionTabContainer.addSubview(connectorBtn)

    connectorBtn.widthAnchor.constraint(equalToConstant: 30).isActive = true
    connectorBtn.heightAnchor.constraint(equalToConstant: 30).isActive = true
    connectorBtn.leftAnchor.constraint(equalTo: actionTabContainer.leftAnchor, constant: 10).isActive = true
    connectorBtn.centerYAnchor.constraint(equalTo: actionTabContainer.centerYAnchor, constant: 0).isActive = true
    connectorBtn.addTarget(self, action: #selector(addConnector(sender:)), for: .touchUpInside)

    addPanReconiser(view: connectorBtn)

    let chooseActionButton = UIButton()
    chooseActionButton.createRectangleButton(buttonPositionX: 0, buttonPositionY: 0, buttonWidth: 0, buttonHeight: 0, buttonTitle: "select", buttonTag: 700)
    chooseActionButton.translatesAutoresizingMaskIntoConstraints = false
    chooseActionButton.backgroundColor = UIColor.gray
    chooseActionButton.layer.cornerRadius = 0

    actionTabContainer.addSubview(chooseActionButton)

    chooseActionButton.widthAnchor.constraint(equalToConstant: 110).isActive = true
    chooseActionButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
    chooseActionButton.leftAnchor.constraint(equalTo: connectorBtn.rightAnchor, constant: 10).isActive = true
    chooseActionButton.centerYAnchor.constraint(equalTo: connectorBtn.centerYAnchor, constant: 0).isActive = true
    chooseActionButton.addTarget(self, action: #selector(addConnector(sender:)), for: .touchUpInside)


    let actionMarkerConnectedLabel =  UILabel()
    actionMarkerConnectedLabel.createLabel(labelPositionX: 0, labelPositionY: 0, labelWidth: 0, labelHeight: 0, labelTitle: "name")
    actionMarkerConnectedLabel.backgroundColor = UIColor.gray
    actionMarkerConnectedLabel.translatesAutoresizingMaskIntoConstraints = false
    actionMarkerConnectedLabel.textAlignment = .center

    connectorBtn.addSubview(actionMarkerConnectedLabel)

    actionMarkerConnectedLabel.widthAnchor.constraint(equalToConstant: 100).isActive = true
    actionMarkerConnectedLabel.heightAnchor.constraint(equalToConstant: 32).isActive = true
    actionMarkerConnectedLabel.leftAnchor.constraint(equalTo: chooseActionButton.rightAnchor, constant: 10).isActive = true
    actionMarkerConnectedLabel.centerYAnchor.constraint(equalTo: connectorBtn.centerYAnchor, constant: 0).isActive = true

}

我有自己的扩展名来创建矩形和其他可能令人困惑的形状。

感谢任何建议

1 个答案:

答案 0 :(得分:0)

我认为如果使用表视图并通过indexPath控制它们会更好。如果没有,您可以创建一个变量来保留“ Count”,然后在创建新变量时:

count += 1
button1.tag = count
button2.tag = count