3

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?

Wain
  • 118,658
  • 15
  • 128
  • 151
h999
  • 395
  • 1
  • 5
  • 21

2 Answers2

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.

enter image description here

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:

iOS Simulator

David Nix
  • 3,324
  • 3
  • 32
  • 51