All my IBOutlet are nil in viewDidLoad
Asked Answered
F

3

17

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 ?

Firebox answered 12/2, 2014 at 8:49 Comment(2)
MyViewController1 *vc = [[MyViewController1 alloc] initWithNibName:@"younibname"]; Have it a try.Shell
How about MyViewController1 *vc = [[MyViewController1 alloc] initWithNibName:@"MySuperViewController1"];Shell
W
14

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;
}
Warehouse answered 12/2, 2014 at 8:51 Comment(3)
Thank you. I've edited my question : the nib belongs to the superclass, and not the subclass.Firebox
Then you should be able to do MyViewController1 *vc = [[MyViewController1 alloc] initWithNibName:@"MySuperViewController1"]Warehouse
initWithNibName: isn't defined.Midweek
R
3

I had the same problem after after breaking my head i realised naming the xib same as the class name solved my problem.

Raasch answered 15/7, 2016 at 6:1 Comment(1)
Thank you! You just saved me possibly hours of going crazy.Giddy
B
1

Check whether IBoulet is linked properly with xib or not. Also check the files owner of your xib.

If your class name or xib name is changed Try to allocate you viewcontroller with proper xibName

MyViewController1 *vc = [[MyViewController1 alloc] initWithNibName:@"NibName" bundle:nil];
Bosun answered 12/2, 2014 at 8:59 Comment(2)
Thank you. I've edited my question : the nib belongs to the superclass, and not the subclass.Firebox
Have you ever renamed your class or nib name? also Check your filesowner as I mentioned before..Bosun

© 2022 - 2024 — McMap. All rights reserved.