Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use awakeFromNib , initWithWindow , windowDidLoad methods?

Tags:

xcode

macos

cocoa

Can you please explain me when we have to use the methods ? and also in what sort of situation should we use which method ? Right now i test which works and use that (yea i am a beginner).. Thanks. I am developing for MAC.

like image 356
Gaurav_soni Avatar asked Jan 16 '23 12:01

Gaurav_soni


1 Answers

First, you should never call awakeFromNib and windowDidLoad yourself. You can implement them in your custom classes, and then Cocoa will call them at the appropriate time.

awakeFromNib works for all objects loaded from a nib archive, not just windows and window controllers. It's a good general place to do setup—it's safe (you're guaranteed that the object is fully loaded, has returned from its init call, and has all of its outlets set), but still pretty early.

windowDidLoad (and windowWillLoad) works for all windows, whether loaded from a nib archive or created on the fly. But it's not called on the window, it's called on the window's controller. (Usually you're not creating your own NSWindow subclass, but you are creating your own NSWindowController subclass.) If you have setup code that depends on the window being loaded, you should put it here (but it's actually not that critical in the simple cases, because as soon as you try to access the window property, it will be created).

initWithWindow: is something you do call yourself, but a beginning Cocoa programmer probably doesn't ever want to do so.

You should probably read some of the guides that come with Xcode. If you want a document-based app, start with "Document-Based App Programming Guide for Mac". If you want a single-window utility app, you'll still need to learn about MVC and so on, so you might actually want to build a document-based app first to learn your way around.

Also, if you want to understand the sequence of events, override every message you can, and add something like NSLog(@"%s", __FUNCTION__); and your syslogs will reveal everything.

like image 121
abarnert Avatar answered Feb 20 '23 18:02

abarnert