Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. class ViewController : UIViewController {
  2.  
  3. //MARK: - outlets
  4. @IBOutlet weak var firstView: FirstUiview!
  5.  
  6. }
  7.  
  8. class FirstUiview : UIView {
  9.  
  10. //creating an instance of secondUiView
  11. lazy var mySecondView: SecondViewClass = {
  12. let dv = SecondViewClass()
  13. dv.backgroundColor = UIColor.red
  14. return dv
  15. }()
  16.  
  17.  
  18. //sometime later by clicking on a button
  19. self.addSubview(mySecondView)
  20.  
  21. //a button will be tapped to remove mySecondView;
  22. //later will be called at some point upon tapping:
  23.  
  24. func removingSecondViewByTapping() {
  25. if mySecondView.isDescendant(of: self) {
  26. mySecondView.removeFromSuperview()
  27. }
  28. }
  29.  
  30. }
  31.  
  32. class SecondViewClass : UIView {
  33.  
  34. //in this class I create bunch of uiview objects like below:
  35. lazy var aView : UIView = {
  36. let hl = UIView()
  37. hl.tag = 0
  38. hl.backgroundColor = UIColor.lightGray
  39. return hl
  40. }()
  41.  
  42. self.addSubview(aView) //... this goes on and I add other similar views the same way.
  43.  
  44. //creating an instance of thirdView
  45. var let thirdView = UIView()
  46. self.addSubview(thirdView)
  47.  
  48. }
  49.  
  50. class FirstView: UIView {
  51.  
  52. weak var secondView: SecondView? // note the `weak` reference, which is obviously an optional
  53.  
  54. //sometime later by clicking on a button
  55.  
  56. func doSomething() {
  57. let subview = SecondView()
  58. subview.backgroundColor = .red
  59. self.addSubview(subview)
  60. secondView = subview
  61. }
  62.  
  63. // a button will be tapped to remove secondView;
  64. // later will be called at some point upon tapping ...
  65.  
  66. func removingSecondViewByTapping() {
  67. secondView?.removeFromSuperview()
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement