6

I have created a new project in xcode 4.5 and I have called my viewcontroller by

-(id)initWithNibName:bundle:

from appdelegate as it called in default project template and i am not using storyboard but awakeFromNib was not called I have also searched but not able to understand why awakeFromNib is not calling.

By apple documentation for -(void)awakeFromNib "Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file." "Objects that conform to the NSCoding protocol (including all subclasses of UIView and UIViewController) are initialized using their initWithCoder: method. All objects that do not conform to the NSCoding protocol are initialized using their init method. After all objects have been instantiated and initialized, the nib-loading code reestablishes the outlet and action connections for all of those objects. It then calls the awakeFromNib method of the objects."

My question is that Why awakeFromNib is not calling when xib file is loaded? Is i am doing something wrong?Please elaborate

codester
  • 36,891
  • 10
  • 74
  • 72

2 Answers2

11

awakeFromNib: is called only if the view controller is inside the nib file.
In this case you init the view controller with init initWithNibName:bundle, so the content of the view controller is inside the nib, not the view controller itself.
If you open the xib file, drag an object inside the xib and set the class to be your view controller class name, then awakeFromNib will be called (this case you don't need to initialize manually the view controller).
So keep in mind the difference between having a view controller in a xib file, and having a view controller not in a xib file, but with all it's contents loaded from a xib file.

PS: I used nib and xib interchangeably.

EDIT
I forgot to tell you how to go around this.Override viewDidLoad to do things with the initialized outlets.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • but by apple documentation it should be called as it have xib and xib is unarchived and loaded. – codester Dec 05 '12 at 18:46
  • Apple documentation says: "Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file.".Your view controller hasn't been loaded from a nib file.Open your nib file and you'll see no view controller instances. – Ramy Al Zuhouri Dec 05 '12 at 19:06
  • I think that Ramy Al Zuhouri is right: `initWithNibName` *creates* a view controller and *associates* the nib file with the controller. The controllers *view* is loaded from the nib file when needed. – Martin R Dec 05 '12 at 20:34
  • Thanks for your response.I have little doubt i have made my `viewcontroller` object class(in xib) same as my `fileowner` and it is working fine.How is this possible?Can you please explain it little bit? – codester Dec 06 '12 at 18:07
  • By default in a view based application the file owner of the xib in the view controller class.The problem is that Apple's documentation is not very clear about saying what's the file owner, read here: http://crazyviraj.blogspot.it/2009/05/cocoa-what-is-files-owner-in-nib.html – Ramy Al Zuhouri Dec 06 '12 at 18:27
  • @RamyAlZuhouri I drag a UIButton inside my nib file, in the Identity tab, I set it to MyButton class. Inside MyButton class, I override awakeFromNib, but it does not get called. Why ? – onmyway133 Nov 19 '13 at 15:18
2

Why not just add the -(id)initWithNibName:bundle: method to your subclass instead of -(void)awakeFromNib?

My "awakeFromNib" methods are called just fine but I load my view controller with:

myViewController *vc = [[[NSBundle mainBundle] loadNibNamed:@"myViewController" owner:self options:nil] objectAtIndex:0];

so I'm wondering if your awakeFromNib isn't called because you're using initWithNibName instead?

EDIT: I just saw your comment that you're adding the awakeFromNib to the ViewController of the default project that is created by XCode. Did you uncheck the "use StoryBoard" box when you created your project? If your nib is inside of the project's main storyboard, there may be some extra hoops you have to jump through.

Travis M.
  • 10,930
  • 1
  • 56
  • 72
  • 1
    In the initWithNibName method like you know, all the views loaded from the xib are not initialized yet.So he probably wants to use awakeFromNib because he needs to deal with all the things retrieved for the xib file.For this purpose he could use viewDidLoad. – Ramy Al Zuhouri Dec 05 '12 at 20:01
  • @Ramy Al Zuhouri thanks for mentioning the viewDidLoad I'd so lost track of what I was doing I missed the obvious! – The Senator Jan 15 '15 at 18:19