0

I am a new developer, two questions as follows

  1. In controller, why can't I call the awakeFromNib method?

    -(void)awakeFromeNib
    {
        NSLog(@"awakeFromNib");// can't be printed ?
    }
    
  2. Do the methods awakeFromNib and initWithNibName , layoutSubviews have some relations? When and where should I use them?

N_A
  • 19,799
  • 4
  • 52
  • 98
skyming
  • 9
  • 4

3 Answers3

0
  1. - (void)awakeFromNib is only called inside of an object that is being directly loaded from a nib or storyboard. This is usually not a UIViewController.

  2. So, the reason awakeFromNib is not on a view controller is that the view controller is the one that is calling its subviews to awake from the nib as set from the method initWithNibName:bundle:. The layoutSubviews method is also called in a subclass of UIView since a view may have subviews that it needs to layout. If I'm not mistaken, the layoutSubviews on a UIView gets called after awakeFromNib.

Hope this helps!

Christian Di Lorenzo
  • 3,562
  • 24
  • 33
0

awakeFromNib is called on the controller after all the connections in it's nib are created and set up.

initWithNibName is the designated initialiser for the class. You call this when you are creating the controller in code.

layoutSubviews is a method that you implement that allows you to give precise layout to subviews.

You should also know about initWithCoder which is the initialiser that is called when the controller is created from a xib file or a storyboard.

Abizern
  • 146,289
  • 39
  • 203
  • 257
0

Your view controller was likely not awaken from a NIB and instead initialized initWithNibName:bundle:. Only objects that are initialized from a NIB get awakeFromNib.

There is a method that gets called when the view is initialized, either from a NIB or loadView: viewDidLoad

Using viewDidLoad accomplishes pretty much the same thing you'd be expecting from awakeFromNib.

You may be able to use awakeFromNib when your view controller is created from a storyboard, as Abizem suggests, but that will still invoke viewDidLoad immediately afterward.

Brian Nickel
  • 26,890
  • 5
  • 80
  • 110