Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ViewController : UIViewController {
- //MARK: - outlets
- @IBOutlet weak var firstView: FirstUiview!
- }
- class FirstUiview : UIView {
- //creating an instance of secondUiView
- lazy var mySecondView: SecondViewClass = {
- let dv = SecondViewClass()
- dv.backgroundColor = UIColor.red
- return dv
- }()
- //sometime later by clicking on a button
- self.addSubview(mySecondView)
- //a button will be tapped to remove mySecondView;
- //later will be called at some point upon tapping:
- func removingSecondViewByTapping() {
- if mySecondView.isDescendant(of: self) {
- mySecondView.removeFromSuperview()
- }
- }
- }
- class SecondViewClass : UIView {
- //in this class I create bunch of uiview objects like below:
- lazy var aView : UIView = {
- let hl = UIView()
- hl.tag = 0
- hl.backgroundColor = UIColor.lightGray
- return hl
- }()
- self.addSubview(aView) //... this goes on and I add other similar views the same way.
- //creating an instance of thirdView
- var let thirdView = UIView()
- self.addSubview(thirdView)
- }
- class FirstView: UIView {
- weak var secondView: SecondView? // note the `weak` reference, which is obviously an optional
- //sometime later by clicking on a button
- func doSomething() {
- let subview = SecondView()
- subview.backgroundColor = .red
- self.addSubview(subview)
- secondView = subview
- }
- // a button will be tapped to remove secondView;
- // later will be called at some point upon tapping ...
- func removingSecondViewByTapping() {
- secondView?.removeFromSuperview()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement