Remove custom UIView randomly Not Working swift 3

Viewed 246

I am building an iOS Application in swift 3, where I am creating dynamic UIView. I need to remove custom view randomly.

class ViewController: UIViewController {
var myView: subView!
var y : CGFloat!
@IBOutlet weak var addButton: UIButton!

override func viewDidLoad() {
    y = 1
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}
func cancelbutton(_ sender: UIButton)
{
    myView.removeFromSuperview()
}
@IBAction func buttonAction(_ sender: Any) {
    y = y + 110
    myView = subView(frame: CGRect(x: 80, y: y, width: 300, height: 100))

    myView.backgroundColor = UIColor.green
    myView.actionButton.addTarget(self, action: (#selector(cancelbutton(_:))), for: UIControlEvents.touchUpInside)
    self.view.addSubview(myView)

}

subview above picture is my custom view

sample output above picture is my sample output

when I click close button only one subview want to be close, which one I was selected. "Thanks in advance"

1 Answers
Related