0

I have a BaseView class which is inherited from UIView like:

@IBDesignable class BaseView: UIView {
 override init(frame: CGRect) {
    super.init(frame: frame)
   }

 required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
   }
}

I set this class for UILabel, UIButton and UITextField in storyboard.

Now, I have to recognise that which one of the UILabel, UIButton or UITextField is called the init?(coder aDecoder: NSCoder).

Is there anyway around this issue?

Update:

It seems that it's not possible to set custom class of type UIView to UILabel so there is no way around this issue.

Mina
  • 2,167
  • 2
  • 25
  • 32
  • you can use the tag value, and check in your initi with coder – Reinier Melian Nov 26 '17 at 10:58
  • but anyway iam not sure about your approach, because the UILabel is a subclass of UIView already – Reinier Melian Nov 26 '17 at 11:02
  • well, the ```self.viewWithTag( _tagNumber_ )``` is always nil. and the tag is always 0 – Mina Nov 26 '17 at 11:29
  • and about the approach I have to say that, I want to changed some shared features between views like `UILable`, `UIButton` and ... in a base class and also by detecting the view change some specific features for each of them. that's why I have to detect the source view – Mina Nov 26 '17 at 11:31
  • Yes I catch your idea @Mina but you can't change the UILabel parent class so you must to implement your UILabel or UIButton or whatever you need, and about the tag i mean the .tag property with is in every UIView class, something like `required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) if(self.tag == 1){print("UILabel")}}` – Reinier Melian Nov 26 '17 at 11:35
  • I've set the tag for the view but when I want to retrieve a view from tag the result is nil. ```self.viewWithTag(10)``` it returns a view but `self.viewWithTag(10) as? UILabel` is nil. – Mina Nov 26 '17 at 12:22

1 Answers1

2

I tried to test the class

if self is UILabel { }

But I get a compiler warning that cast from BaseView to unrelated UILabel always fail

if (self as UIView) is UILabel { }

clears the warning but is of no use.

So a question: you said "I set this class for UILabel, UIButton and UITextField in storyboard"

Could you detail what you did ?

How do you declare the IBOutlet ? BaseView ? What type do you assign in IB to the label ? Because BaseView is not a subclass of UILabel, I could not declare the label as BaseView ?

claude31
  • 874
  • 6
  • 8
  • and the result of the `if (self as UIView) is UILabel` is always nil. – Mina Nov 26 '17 at 12:24
  • Yes. When loading the IBOutlet var labelView: BaseView! : if BaseView is declared as subclass of UILabel, we go through the init?(coder) ; if it is declared as subclass of UIView, not. But then, I changed again declaration of BaseView as UIView ; I get a warning, but it compiles and it goes through the init?(coder) ; I've included a print("From init(coder)") to check. – claude31 Nov 26 '17 at 12:52
  • Could you explain how you "set this class for UILabel, UIButton and UITextField in storyboard" – claude31 Nov 26 '17 at 14:44
  • When I set the BaseView as a custom class for UILabel it was inhertited from UILabel and after that I change the BaseView and changed nothing in storyboard, based on this scenario the decoder init was still called. – Mina Nov 26 '17 at 15:47
  • I feel we are somehow "cheating" the system doing so, as what is declared in code and in IB are not consistent. Finally, I think the only solution is to create a subclass for each: UILabel, UIButton, UITextField, as someone proposed earlier. – claude31 Nov 26 '17 at 16:28
  • Yeah, I had seperate subclasses for each of them, I was thinking about having all of them in one. And finally findout there is no way. – Mina Nov 26 '17 at 16:29