0

Here's my code right now:

@objc protocol BaseView {
  @objc optional var header: UILabel { get set }
  @objc optional var footer: UILabel { get set }
}

class ViewWithHeader: UIView, BaseView {
  @IBOutlet var header: UILabel!
}

class ViewWithFooter: UIView, BaseView {
  @IBOutlet var footer: UILabel!
}


var view: BaseView!
if (header != nil) {
  view = Bundle.main.loadNibNamed("ViewWithHeader", owner: self, option: nil)?.first as! ViewWithHeader
} else if (footer != nill) {
  view = Bundle.main.loadNibNamed("ViewWithFooter", owner: self, option: nil)?.first as! ViewWithFooter
}

view.header?.text = "Header"
view.footer?.text = "Footer"
view.alpha // This line fails because BaseView has no member 'alpha'

My question is how do I extend UIView to include the optional header and footer variables? Maybe I'm doing this completely wrong in which case I'd appreciate a pointer in the right direction.

Rawr
  • 2,206
  • 3
  • 25
  • 53

1 Answers1

1

If I understand your question correctly, You need to make your BaseView a subclass of UIView. Then in your class definition, you can add optional properties as you like. The code will look something like this

class BaseView: UIView {
    var header: UILabel?
    var footer: UILabel?
}

EDIT

Based on your explanation in the comments, I believe you need something like this

class BaseView: UIView {
    @IBOutlet var header: UILabel?
    @IBOutlet var footer: UILabel?
}

class ViewWithHeader: BaseView {
}

class ViewWithFooter: BaseView {
}


var view: BaseView!
if (header != nil) {
  view = Bundle.main.loadNibNamed("ViewWithHeader", owner: self, option: nil)?.first as! ViewWithHeader
} else if (footer != nil) {
  view = Bundle.main.loadNibNamed("ViewWithFooter", owner: self, option: nil)?.first as! ViewWithFooter
}

view.header?.text = "Header"
view.footer?.text = "Footer"
view.alpha // This line fails because BaseView has no member 'alpha'
Malik
  • 3,763
  • 1
  • 22
  • 35
  • When I do this I get warnings on `ViewWithHeader` and `ViewWithFooter` claiming their variables can't be required (let me double check). **EDIT:** It says Cannot override with stored property 'header' AND Setter for 'header' with Objective-C selection setHeader conflicts with setter for 'header' from superclass BaseView with the same Objective-C selector – Rawr Jul 21 '17 at 06:57
  • What exactly is it that you are trying to achieve? Maybe with more information, I can provide a better solution. But with the information at hand and the way the question is posed, this is the only advice I can give. Specifically, what is the point of `ViewWithHeader` and `ViewWithFooter` and what conditions determine if they should be set or not? – Malik Jul 21 '17 at 07:03
  • Basically I'm determining what View I want to use either `ViewWithHeader` or `ViewWithFooter` depending on what variable is set. I want to use these two views interchangably. – Rawr Jul 21 '17 at 07:11
  • Looking at your provided code, I don't see any difference in both the views. They both have one label. Unless they are linked to different views in storyboard. If that is the case, let me know and I'll update my answer. Also, I'm a bit confused regarding your "decider" logic. You are checking for the value of both the labels (which would be nil) first, and then setting both the labels. Could you clarify that as well please? – Malik Jul 21 '17 at 07:13
  • This code is a bit simplified but essentially I've got multiple views that layout the data I've stored in different places. I'm using the variables to determine which view I want to use and then setting the value of those labels after I choose the view. The views are interchangable which is why I want to inherit them from the same base view. – Rawr Jul 21 '17 at 07:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149779/discussion-between-malik-and-rawr). – Malik Jul 21 '17 at 07:38
  • Yea so now it says "Value of type 'BaseView' has no member 'header'". Because the `view` variable is initially set to `BaseView` it doesn't find these members. – Rawr Jul 21 '17 at 07:39