2016-08-02 141 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