1

I have customView called BaseView which has a contentView, In the contentView I am adding all other subviews(UILabel, UIButton, etc) in the override init(frame: CGRect) method.

Now I have 10 subclasses of my BaseView, which also overriding init(frame: CGRect) and calling base class init(frame: CGRect) method.

Here all my subclasses looks similar UI to its BaseView, Now there is one subclass of BaseView doesn't want some of the UI Elements in this base class, but I still need to call superview init(frame: CGRect). How do I change the code without affecting other classes?

Class BaseView: UIView {

let contentView = UIView()

    override init(frame: CGRect) {
       super.init(frame: frame)

       let lbl1 = UILabel()
       contentView.addSubView(lbl1)

       let lbl2 = UILabel()
       contentView.addSubView(lbl2)

       self.addSubView(contentView)

     }

  Class subView1: BaseView {

    override init(frame: CGRect) {
       super.init(frame: frame)

      // This class will show lbl1, lbl2 and lbl3 in the contentview

       let lbl3 = UILabel()
       contentView.addSubView(lbl3) // this contentview is BaseView's ContentView


     }

   // Similarly I have around 10 Subclasses of BaseView which is adding some UI Element to 
    baseview's contentView

   // Question here is, below I am going to create another subclass of BaseView, But I don't 
    want to show lbl1 and lbl2 which is created in my BaseView's contentview

   Class myView: BaseView {

   // this class should not show the base class uilement lbl1 and lbl2, It should show only 
    lbl4 which is created by this class only

    override init(frame: CGRect) {
       super.init(frame: frame)

       let lbl4 = UILabel()
       contentView.addSubView(lbl4) // this contentview is BaseView's ContentView


     }
Caleb
  • 124,013
  • 19
  • 183
  • 272
Ram
  • 269
  • 2
  • 9

2 Answers2

0

How do I change the code without affecting other classes?

You don't. A different approach is needed. Three options are:

  • Don't derive the view in question from BaseView, and just reproduce whatever BaseView functionality you need.
  • Continue to derive from BaseView, and hide or remove the elements you don't need after BaseView's initialization method. The success of this plan will depend on how tolerant BaseView is when elements it expects are missing.
  • Refactor the functionality in BaseView into some new class that has the behavior that's common to all your views, and have your one outlier view and BaseView each descend from that new class.
Caleb
  • 124,013
  • 19
  • 183
  • 272
0

Here is a different approach to implementing the required functionality.

Create a parent class with a content view where you want to show the content.

class SuperView: UIView {
let contentView = UIView()
override init(frame: CGRect) {
    super.init(frame: .zero)
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
    fatalError("init(coder:) has not been implemented")
}

}

Inherit BaseView class from SuperView class and you can inherit other classes from BaseView class.

class BaseView: SuperView {
override init(frame: CGRect) {
    super.init(frame: .zero)
    contentView.addSubview(UILabel()) // UILabel 1
    contentView.addSubview(UILabel()) // UILabel 2
    self.addSubview(contentView)
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
    fatalError("init(coder:) has not been implemented")
}

}

Inherit SubView class from SuperView class to add different properties and functions that you want. Here you don't need to inherit from BaseView class.

class SubView: SuperView {
override init(frame: CGRect) {
    super.init(frame: .zero)
    contentView.addSubview(UILabel()) // UILabel 3
    self.addSubview(contentView)
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
    fatalError("init(coder:) has not been implemented")
}

}

I hope it will help you to implement the required functionality as you want.

Inder Jagdeo
  • 229
  • 2
  • 9