我很新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