Runtime Custom Attribute Availability

I created custom Runtime attributes for my custom UIView in IB, and it is interesting that I can access it in the touchhesBegan: withEvent: method, but not in the initWithCoder: do you know why?

+4
source share
2 answers

This is because views from IB end with loading after initWithCoder . The following will help you understand the process. This is explained for UIViewControllers , but the concept is the same (from the ViewController Programming Guide ):

When you create a view controller in a storyboard, the attributes you configure in Interface Builder are serialized to the archive. Later, when an instance of the view controller is created, this archive is loaded into memory and processed. The result is a set of objects whose attributes correspond to those that you set in Interface Builder. The archive is loaded with a view manager call to the initWithCoder: method.

+5
source

If you need to access values ​​when setting up your view instead of processing in initWithCode: use awakeFromNib

+9
source

Source: https://habr.com/ru/post/1443171/


All Articles