How can I initialize a custom GUI class?
I'm developing an iPad app.
Now I ra开发者_如何学Gon into the following problem. I created a "Custom Class" for a UIScrollView
. So in my nib file I have added a default UIScrollView
and set the Custom Class
to MultiSelectView (which is the custom class I created). Screenshot here.
So in my MultiSelectView I have added some methods that I want to use. This works!
The issue is that I'm wondering how I can initialize certain objects that I need in these methods. It seems like - (id)initWithFrame:(CGRect)frame {}
is not called, and neither is - (void) viewDidLoad {}
.
Thus, is there a way to initialize a custom (GUI) class?
When you unarchive a view from a .xib file, it is not sent -initWithFrame:
, as you've noticed. Instead, it's sent -initWithCoder:
.
So, if you've got any UIView subclass in a .xib file that needs custom initialization, you'll need to override -initWithCoder:
as well as (or instead of) -initWithFrame:
.
Looks like you need initWithCoder
, it is called when object is loaded from NIB
Or, better, awakeFromNib
. The difference is that awakeFromNib
is called when all outlets are connected.
精彩评论