Reputation: 395
I have UIViewController
and UIView
subclass (lets say ViewSubClass
). I am using story board. In mainStoryBoard.storyBord
, I put one UIView
and set custom class ViewSubClass
. Now I do not want to create instance of ViewSubClass
in UIViewController
like ViewSubClass *subClassInstance = [[ViewSubClass alloc] init]
. Without creating any instance of ViewSubClass
in ViewController
, i want to call initWithCoder
method. When I run my application , initWithCoder
method of ViewSubClass
should be called. How can I achieve that?
Upvotes: 3
Views: 4592
Reputation: 3334
Occasionally, I've seen initWithCoder:
not called even though I have the subclass view set up in Interface Builder as shown in the accepted answer.
Blowing away XCode's and the Simulator's caches solves the problem for me.
Command + Shift + k
is usually good enough for XCode.
Choose "Reset Content and Settings" for the iOS Simulator:
Upvotes: 2
Reputation: 2924
Add below code in your view class .m file
-(id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code
}
return self;
}
And Add file name in view Class as below screen shot.
Upvotes: 4