문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top