2016-08-02 75 views
0

我很新swift。我使用subview創建了multiple textfield and button。我ViewController的輸出低於: - here單擊按鈕時如何刪除按鈕?

現在我需要刪除"-"按鈕和它對應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() 
} 

任何幫助,將不勝感激......

+0

嘗試使用按鈕標籤? –

+1

你可以顯示你的代碼 –

+0

我已經添加了我的代碼 – ripa

回答

1

愛姆卡是正確的!試試這個:

import UIKit 

class ViewController: UIViewController { 

    var y:CGFloat = 100 
    var textFields = [UIButton : UITextField]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    @IBAction func onAddMoreButtonPressed(sender: AnyObject) { 
     let newButton = UIButton(frame: CGRect(x: 50, y: y, width: 150, height: 20)) 


     newButton.setTitle("New button", forState: .Normal) 
     newButton.backgroundColor = UIColor.blueColor() 
     newButton.addTarget(self, action: #selector(ViewController.onNewButtonPressed(_:)), forControlEvents: .TouchUpInside) 

     self.view.addSubview(newButton) 

     let newTextField = UITextField(frame: CGRect(x: 200, y: y, width: 150, height: 20)) 
     newTextField.text = "New text field" 
     self.view.addSubview(newTextField) 

     textFields[newButton] = newTextField 

     y += 20 
     if y > self.view.frame.height { 
      y = 100 
     } 
    } 

    func onNewButtonPressed(sender: UIButton) { 
     textFields[sender]?.removeFromSuperview() 
     sender.removeFromSuperview() 
    } 

} 
+0

此概念正常工作。但問題是Y不更新。所以差距仍然存在。有沒有解決方案? – ripa

+0

什麼是不更新?查看,字典? – eMKa

+0

視圖。選中此項 - [http://imgur.com/a/dbOKx] – ripa

0

您可以覆蓋任何視圖控制器的方法touchesBegan。假設你是存儲您的按鈕陣列某處

let buttons : [UIButton] = [] 

,你可以做到以下幾點:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 

    super.touchesBegan(touches, withEvent: event) 

    guard let touch: UITouch = touches.first else { 
     return 
    } 

    for button in buttons { 
     if touch.view == button { 
      print("This is the button you tapped") 
      button.removeFromSuperview() 
     } 
    } 

} 
+0

我在這個函數中收到「uncaught exception」錯誤。 – ripa

+0

好吧,這個答案是基於你在某處存儲你的視圖的假設,並且是爲了成爲一個指南,而不是最終的答案。您需要提供有關該錯誤的更多詳細信息,以便我可以幫助您。 – Armin

+0

libC++ abi.dylib:以NSException類型的未捕獲異常終止 - 此錯誤 – ripa