Swift - 将以编程方式创建的按钮和标签相互链接
Swift - Linking programmatically created buttons and labels to each other
我正在以编程方式创建所有内容,但在 link 多个按钮对 select 自定义操作作出反应时遇到问题。
蓝色按钮是以编程方式创建的,我使用标签来跟踪按下了哪个按钮。 select 编辑蓝色按钮时,会弹出一个操作菜单,可以通过按添加图标对按钮进行 link 操作。
您可以 select 您想要的操作,方法是单击 "select",然后按下并从 "o" 按钮拖动以创建连接器。
我在字典中存储了 selected 操作以及连接到它的按钮
问题是当以编程方式创建时,我如何才能 link 同一行的 link 按钮、"select" 按钮和 "name" 标签?我没有使用 tableview 来创建操作。这样会更容易使用吗?
这将创建操作行
// 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
}
我有自己的扩展来创建矩形和其他看起来可能令人困惑的形状。
感谢您的任何建议
我认为如果使用 table 通过 indexPath 查看和控制它们会更好。如果没有,您可以创建一个变量来保留 "Count" 然后在创建新时:
count += 1
button1.tag = count
button2.tag = count
我正在以编程方式创建所有内容,但在 link 多个按钮对 select 自定义操作作出反应时遇到问题。
蓝色按钮是以编程方式创建的,我使用标签来跟踪按下了哪个按钮。 select 编辑蓝色按钮时,会弹出一个操作菜单,可以通过按添加图标对按钮进行 link 操作。
您可以 select 您想要的操作,方法是单击 "select",然后按下并从 "o" 按钮拖动以创建连接器。
我在字典中存储了 selected 操作以及连接到它的按钮
问题是当以编程方式创建时,我如何才能 link 同一行的 link 按钮、"select" 按钮和 "name" 标签?我没有使用 tableview 来创建操作。这样会更容易使用吗?
这将创建操作行
// 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
}
我有自己的扩展来创建矩形和其他看起来可能令人困惑的形状。
感谢您的任何建议
我认为如果使用 table 通过 indexPath 查看和控制它们会更好。如果没有,您可以创建一个变量来保留 "Count" 然后在创建新时:
count += 1
button1.tag = count
button2.tag = count