我很新swift。我使用subview
创建了multiple textfield and button
。我ViewController
的输出低于: - 单击按钮时如何删除按钮?
现在我需要删除"-"
按钮和它对应textfield
被点击时它。 但我无法检测到哪个按钮被点击。 这是我的代码:
var y: CGFloat = 190
var by: CGFloat = 192
@IBAction func addRow(sender: AnyObject) {
y += 30
by += 30
let textFiled = UITextField(frame:CGRectMake(50.0, y, 100.0, 20.0))
textFiled.borderStyle = UITextBorderStyle.Line
let dunamicButton = UIButton(frame:CGRectMake(155.0, by, 15.0, 15.0))
dunamicButton.backgroundColor = UIColor.clearColor()
dunamicButton.layer.cornerRadius = 5
dunamicButton.layer.borderWidth = 1
dunamicButton.layer.borderColor = UIColor.blackColor().CGColor
dunamicButton.backgroundColor = .grayColor()
dunamicButton.setTitle("-", forState: .Normal)
dunamicButton.addTarget(self, action: #selector(removeRow), forControlEvents: .TouchUpInside)
self.view.addSubview(textFiled)
self.view.addSubview(dunamicButton)
}
func removeRow(sender: UIButton!) {
print("Button tapped")
self.view.removeFromSuperview()
}
任何帮助,将不胜感激......
尝试使用按钮标签? –
你可以显示你的代码 –
我已经添加了我的代码 – ripa