Frank Boccia Frank Boccia - 5 months ago 12
iOS Question

Removing a UIView and bringing back the UIView in swift

I am using a UIView not a view controller/ I am removing the view once the delete button is clicked. I am trying to make the view come back when a user wants the view back.
example : myView.removeFromSuperview()
Is there anyway to bring the view back?
In swift thank you guys!

@IBOutlet weak var brokeView: UIView!
@IBOutlet weak var deleteButton: UIButton!

@IBAction func deleteViewButton(sender: AnyObject) {
if brokeView != nil {
brokeView.removeFromSuperview()
}

Answer
@IBOutlet weak var brokeView: UIView!

you already have a reference to the view brokeView which you are going to remove and add again, but its weak so it will be deallocated once it is removed from superView. Since you need this brokeView to add back make it strong.

@IBOutlet var brokeView: UIView!

Now you can add it back like

view.addSubview(brokeView)