IOS Swift3 how to delete custom UIView randomly

I am creating an iOS app in swift 3 where I am creating dynamic UIViews. I need to delete a custom view in a random way. Please help me, I have been stuck with this for a long time. thanks in advance

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)
    }
}

      

enter image description here

enter image description here

enter image description here

As you can see in the above image, when I click Close, the SubView (custome View) is closed, but where the MyView with green color does not go and stays there. Someone please help .........

required init?(coder aDecoder: NSCoder)
   {
       super.init(coder: aDecoder)
       Bundle.main.loadNibNamed("subView",owner: self, options:nil)
       self.addSubview(self.views)
       Bundle.main.loadNibNamed("subView",owner: self, options:nil)
       self.addSubview(self.views)

   }
   override init(frame: CGRect)
   {
       super.init(frame: frame)
       Bundle.main.loadNibNamed("subView", owner: self, options: nil)
       views.frame = bounds
       self.addSubview(self.views)
   }

   @IBAction func buttonAction(_ sender: Any) {
       views.removeFromSuperview()

   }

      

+3


source to share


3 answers


What you are doing wrong is that you are adding multiple subViews that you have selected and not deleting. Please change your code as shown below. The thing is, whenever you add a new one subView

, you also set its tag value and when you select a view to remove its tag value and put a statement if

on that tag value.



class ViewController: UIViewController { 
    var myView: subView! 
    var y : CGFloat! 
    var tag : Int = 0 
    @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)
    {
                let selectViewTagValue : Int = sender.tag /// save the selected view tag value
                for object in self.view.subviews {

                if ((object is subView) && object.tag == selectViewTagValue)
                {
                    object.removeFromSuperview()
                }
            }
    }
    @IBAction func buttonAction(_ sender: Any) {
        y = y + 110
        myView = subView(frame: CGRect(x: 80, y: y, width: 300, height: 100))
        myView.tag = tag 
        myView.actionButton.tag = tag
        tag = tag + 1
        myView.backgroundColor = UIColor.green
        myView.actionButton.addTarget(self, action: (#selector(cancelbutton(_:))), for: UIControlEvents.touchUpInside)
        self.view.addSubview(myView)

    }

      

+2


source


I was able to fix the deletion issue, but I am currently unable to move the customs positions as shown in the image below. Please help me with this.



enter image description here enter image description here

+1


source


Try changing your undo action like this:

func cancelbutton(_ sender: UIButton)
{
    if let myView = sender.superview {
        myView.removeFromSuperview()
    }
}

      

Just remove the button container view, not the global myView.

0


source







All Articles