Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
655 views
in Technique[技术] by (71.8m points)

ios - All my IBOutlet are nil in viewDidLoad

I've created a UIViewController that we can call MyViewController1. When I call MyViewController1, all my IBOutlet are nil in viewDidLoad (and in the rest of the code too).

When I create this controller by doing
MyViewController1 *vc = [[MyViewController1 alloc] init],

if I replace MyViewController1 by an other one, for example MyViewController2, it works. So I guess the problem is really in MyViewController1.

Last thing you might want to know, is that MyViewController1 is actually a subclass of MySuperViewController1 that is a UIViewController.

Thanks for your help !


EDIT

I realized my case was maybe more complicated. Here are my exact files :

// MySuperViewController1

MySuperViewController1.h

MySuperViewController1.m

MySuperViewController1.xib

// MyViewController1

MyViewController1.h

MyViewController1.m

So the nib belongs to the superclass, and not the subclass. Can I do that ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should probably use :

MyViewController1 *vc = [[MyViewController1 alloc] initWithNibName:@"MyViewController1" bundle:nil]

calling init won't do the match with your xib file and won't alloc your differents IBOutlet

EDIT :

There are two possibles solutions :

First is calling init with super nibName :

MyViewController1 *vc = [[MyViewController1 alloc] initWithNibName:@"MySUperViewController1" bundle:nil]

The second is calling the super initWithNibName: in child init method :

-(id)init {
   if (self = [super initWithNibName:@"MySuperViewController1" bundle:nil]) {
        // Init
   }
   return self;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...