0

Having discovered that awakeFromNib is begin called multiple times, I tried to implement loadView in the following way to prevent (nib loading) initialization from repeatedly occurring, with:

- (void)loadView {
    [self viewWillLoad];
    [super loadView];
    [self viewDidLoad];
}

Looks like a good trick to allowing certain arrays and properties to be set-up in viewWillLoad, but loadView absolutely won't be called.

Why?

I've done much research about this here and through google.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
David
  • 3,285
  • 1
  • 37
  • 54
  • Are you using NSViewController? – Parag Bafna Aug 27 '13 at 14:47
  • When are you expecting `loadView` to be called? – Peter Hosey Aug 27 '13 at 20:01
  • @PeterHosey I guess the 'new' answer is never. If its not a good idea to do initialization in the init method, then where is a good place using a view or window controller? – David Aug 27 '13 at 23:21
  • @PeterHosey I see your edit to move OSX. I intended for that to help others reading to quickly know it's a question about OS X. There are many questions with a similar topic regarding iOS, but few about Mac. I bit on many, which caused a charged down the wrong road. – David Aug 27 '13 at 23:27
  • @PeterHosey Would you consider making an answer with your last comment 'My theory of why...' below? – David Aug 28 '13 at 13:32

2 Answers2

0

You're not receiving a loadView message because you have this VC and its view in the same nib, with the VC's view outlet set to the view. Since the VC already has a view, it has no reason to go load another one.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
-1

loadView is typically not called if you are using a nib (since view is already set). But the real question is why you're trying to fight the view loading process this way. If awakeFromNib is being called multiple times, that suggests you have multiple instances of this class. Each will get a call to awakeFromNib (that is expected behavior). If this is surprising, you should dig into why you have multiple instances. But you shouldn't try to subvert the view-loading mechanism like this.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Multiple VCs is one possibility, but not the only one: A single instance of a VC that is archived in one nib and owns another nib will receive two `awakeFromNib` messages. – Peter Hosey Aug 27 '13 at 21:14
  • Also, whether “`view` is already set” depends on what you mean by “using a nib”. If the VC and view are both in the same nib (the VC in this case should not own any nib), then yes, `view` is already set to the view to which that outlet is connected. If the VC owns a nib and that nib is where the view is, then `view` is not already set, so [`loadView` is called by `view` the first time something tries to access this VC's view](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSViewController_Class/Introduction/Introduction.html#//apple_ref/occ/instm/NSViewController/view). – Peter Hosey Aug 27 '13 at 21:17
  • 1
    @PeterHosey & Rob. The multiple calls to `awakeFromNib`, to the best of my understanding is because the one tableview in this VC has a subclassed cell (view based). For each cell that's created, `awakeFromNib` gets called. Interestingly its the same instance for every call. I don't see the point of building a few arrays over and over again... What if this was a large table? I was hoping to find a more elegant way of initializing certain data for the view. – David Aug 27 '13 at 23:10
  • 2
    @David: Nobody is suggesting that you should build your arrays repeatedly. Your `loadView` approach should work (going by the documentation) if the VC had a nib of its own, or if it created its view programmatically (in the overridden `loadView`). My theory of why you're not receiving a `loadView` message is that you have this VC and its view in the same nib, with the VC's `view` outlet set to the view; thus, the VC already has a view, so it has no reason to go load another one. – Peter Hosey Aug 28 '13 at 08:06
  • Thanks Rob. The picture and question are becoming clearer. The purpose of `awakeFromNib` is not to set up (data) arrays for the view... I've asked the wrong question. Btw, this was less about fighting the view loading process - more about learning the overall process. Per my comment 2 above, there is another big reason for `awakeFromNib` to be called. For those of us who are new and without experience, combining details in the documentation doesn't just happen. – David Aug 28 '13 at 13:28