Search code examples
swiftxcodeuibuttontagsprogrammatically-created

Swift - Linking programmatically created buttons and labels to each other


I am creating everything programmatically and I am having issues linking multiple buttons to react to a selected custom action.

the blue button is created programmatically and I use tags to keep track which one is pressed. When the blue button is selected, an action menu pops up which can link actions to the button by pressing the add icon.

You can select the action you want by clicking on "select" then press and drag from the "o" button to create the connector.

I store in a dictionary the selected action and which buttons are connected to it

The issue is how I can link the "o" button, "select" button and "name" label from the same row to each other when they are created programmatically? I am not using tableview to create the actions. Would that be easier to use?

enter image description here

This create the action row

// 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

}

I have my own extensions to create rectangles and other shapes which might look confusing.

Thanks for any recommendations


Solution

  • I think it will better if using table view and control them by indexPath. If not, you can create a variable to keep "Count" then when create new :

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