【问题标题】:Custom UIView Implemenation自定义 UIView 实现
【发布时间】:2020-06-16 05:42:06
【问题描述】:

我有一个名为 BaseView 的 customView,它有一个 contentView,在 contentView 中,我在 override init(frame: CGRect) 方法中添加了所有其他子视图(UILabel、UIButton 等)。

现在我的 BaseView 有 10 个子类,它们也覆盖 init(frame: CGRect) 并调用基类 init(frame: CGRect) 方法。

这里我所有的子类看起来都类似于它的BaseView UI,现在BaseView 的一个子类不想要这个基类中的一些UI 元素,但我仍然需要调用superview init(frame: CGRect)。如何在不影响其他类的情况下更改代码?

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


     }

【问题讨论】:

    标签: ios swift uiview subclass


    【解决方案1】:

    如何在不影响其他类的情况下更改代码?

    你没有。需要一种不同的方法。三个选项是:

    • 不要从BaseView 派生有问题的视图,而只是复制您需要的任何BaseView 功能。
    • 继续从BaseView派生,在BaseView的初始化方法后隐藏或移除不需要的元素。该计划的成功将取决于BaseView 在预期元素缺失时的容忍度。
    • BaseView 中的功能重构为某个新类,该类具有所有您的视图所共有的行为,并且您的一个异常视图和BaseView 都来自该新类。

    【讨论】:

      【解决方案2】:

      这是实现所需功能的另一种方法。

      创建一个带有内容视图的父类,您希望在其中显示内容。

      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")
      }
      

      }

      从SuperView类继承BaseView类,你可以从BaseView类继承其他类。

      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")
      }
      

      }

      从 SuperView 类继承 SubView 类以添加您想要的不同属性和功能。这里不需要继承 BaseView 类。

      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")
      }
      

      }

      我希望它能帮助您实现所需的功能。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多