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?
Asked
Active
Viewed 4,519 times
3
-
Just set your view as ViewSubClass from storyboard interface builder. And initWithCoder will call when app lunch and you access this view... – Hindu Oct 03 '13 at 10:53
-
but it is not calling initWithCoder method... – h999 Oct 03 '13 at 10:54
-
Can you post your screen shot of interface builder – Hindu Oct 03 '13 at 10:55
-
And you code for `initWithCoder:` – Wain Oct 03 '13 at 10:55
2 Answers
4
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.

Hindu
- 2,894
- 23
- 41
-
Actually i have created library...ViewSubClass is file of our library.and I am using it into single View class application...Is there any chance if i use library then it can not call initWithCoder method? – h999 Oct 03 '13 at 11:12
-
It should call each time if you follow same steps as above either View file in lib or seperate – Hindu Oct 03 '13 at 11:16
2
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:

David Nix
- 3,324
- 3
- 32
- 51