It just sits there… How do I run code independently of UI actions (as in, not only in response to a button push)? I’m trying to initialize some objects and run some scripts BEFORE i awakeFromNib. How do I do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
None, because there isn’t one.
For one thing, the .m file is not the class. Neither is the .h. The practice of putting a class’s
@interfacein a .h (header) file and its@implementationin a .m (implementation) file is a convention, nothing more. It’s a good convention, but it’s not enforced by the language.So, a .m file contains zero or more class implementations. Usually, it’s exactly one. Sometimes it’s two, where the other is a small private class whose
@interfaceis also in the same file.main.m contains no classes. It usually only contains one function, which is named
main.maincomes from C (of which Objective-C is a superset), and is the entry point of the program: It’s where the program starts running. Everything happens here, or in a function called from here. A command-line tool will usually exit here as well, bymainreturning.Most
mainfunctions in Cocoa apps are the default one that comes with the project template; this implementation simply tail-callsNSApplicationMain, which sets up the shared NSApplication object and starts it running. That function never returns; when the user quits a Cocoa app, the process simply exits.You may want to read this list of important facts about Cocoa and Objective-C that I wrote. It sounds like you have some misconceptions (probably brought over from another framework you’re more familiar with) that that list can clear up for you.
That depends on when you are trying to do it. A periodic action, for example, would be a job for a timer.
You could do it in
initWithCoder:, which is sent to every non-view-layer object instantiated from an archive, including yours. Nibs are archives, too.You might also consider being the application delegate and implementing a
applicationWillFinishLaunching:method.A third way would be to stuff the code in
main. Note that practically any Cocoa code here is likely to log “no autorelease pool” warnings unless you wrap it in an@autoreleasepoolstatement; other solutions don’t have this problem because NSApplication has already created an autorelease pool for you.