简体   繁体   中英

IOS Swift3 how to Remove Custom UIView randomly

I am building an iOS Application in swift 3, where I am creating dynamic UIViews. I need to remove custom view randomly. Please help me I am 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)
    }
}

在此输入图像描述

在此输入图像描述

在此输入图像描述

As you can see in the above image when i click on Close the SubView(custome View) closes, but where as 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()

   }

The thing you are doing wrong is that you add multiple views of subView that why you selected does not remove. Please modify your code like given below. The thing I have done is that whenever you will add new subView you will also set its tag value and when you select view to remove its tag value and place an if statement 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)

    }

I managed to fixed the delete issue, but currently I am unable to relocate the positions the customs views, as shows in the picture below, Kindly help me with this.

在此输入图像描述 在此输入图像描述

Try to change cancel action like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM