swift 3 如何以编程方式添加 UITextView 然后在单击按钮时将其删除?

swift 3 How to add UITextView programmatically then remove it when click the button?

我是 swift 上的新手 3. 我有这种情况:我需要动态创建 textview 以及它下面的删除按钮...所以有按钮动态添加字段 (textview) 然后有按钮在它下面删除它..我把它们放在视图上..假设"placeholderCD"

enter image description here

但问题是..单击删除按钮时我需要执行什么操作或执行什么操作?因为我们不能分配动态按钮的 id 对吗?所以我的代码如下:

func AddCommentItem(sender:UIButton){
    /** CREATE TEXT VIEW **/
    let textView: UITextView = UITextView(frame: CGRect(x: 0, y: (newX + 20), width: 311.00, height: 50.00));
    textView.textAlignment = NSTextAlignment.justified
    //textView.backgroundColor = UIColor.lightGray
    let borderColor = UIColor.lightGray
    textView.layer.borderColor = borderColor.cgColor
    textView.layer.borderWidth = 1.0
    textView.tag = tagname
    /** CREATE TEXT VIEW **/

    /** CREATE REMOVE TEXT VIEW **/
    let btnRemove: UIButton = UIButton(frame: CGRect(x: 240, y: (newX + 52), width: 100, height: 50))
    //btnRemove.backgroundColor = UIColor.green
    btnRemove.setTitle("Remove", for: .normal)
    btnRemove.titleLabel?.font = btnRemove.titleLabel?.font.withSize(10)
    btnRemove.tag = tagname

    btnRemove.setTitleColor(UIColor.gray, for: .normal)
    btnRemove.addTarget(self, action: #selector(btnRemoveComment), for: .touchUpInside)
    //btnRemove.tag = 1

    /** CREATE REMOVE TEXT VIEW **/

    cell.placeholderCD.addSubview(textView)
    cell.placeholderCD.addSubview(btnRemove)
}

func btnRemoveComment(Sender:UIButton) {
   /** HOW I REMOVE THE Related text view object with this action **/
}

你可以通过标签移除textview

cell.placeholderCD.viewWithTag(yourTag)?.removeFromSuperview()