Pergunta

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?

Foi útil?

Solução

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

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top