I want to load some data from mysql into my cocoa application view before the application starts.
我想在应用程序启动之前将一些数据从mysql加载到我的cocoa应用程序视图中。
I am sure that this should happen in the controller so that it can send the required data to the view.
我确信这应该发生在控制器中,以便它可以将所需的数据发送到视图。
I am looking for a method or common technique that is used for this sort of thing.
我正在寻找一种用于此类事情的方法或常用技术。
Many Thanks
3 个解决方案
#1
Cocoa gives you many places to perform tasks before and after objects are loaded from a nib, but it's important to read the documentation carefully to make sure things are happening in the order you expect. Usually I use the following strategy when I'm working on a Cocoa application:
在从笔尖加载对象之前和之后,Cocoa为您提供了许多执行任务的位置,但仔细阅读文档以确保按照您期望的顺序进行操作非常重要。通常我在使用Cocoa应用程序时使用以下策略:
- Where appropriate I implement the +(void)initialize method, which is called before any instances of a class are created. I'll probably set the app's default preferences here, for example.
- In my application controller (app delegate), I implement the applicationDidFinishLaunching: delegate method to load my data file. If this works okay, I then create the window controller(s) and display any windows I want to show at launch.
- In the window/view controllers, I override windowDidLoad: or loadView to perform tasks involving objects loaded from a nib. If I need to create any instance variables that don't involve the nib, I also override the init method and do that there.
- If I need to do anything in my view objects after they're loaded from a nib, I'll override awakeFromNib.
在适当的地方,我实现了+(void)initialize方法,该方法在创建类的任何实例之前调用。例如,我可能会在这里设置应用程序的默认首选项。
在我的应用程序控制器(app delegate)中,我实现了applicationDidFinishLaunching:delegate方法来加载我的数据文件。如果这样可行,我会创建窗口控制器并显示我想在启动时显示的任何窗口。
在窗口/视图控制器中,我覆盖windowDidLoad:或loadView来执行涉及从笔尖加载的对象的任务。如果我需要创建任何不涉及nib的实例变量,我也会覆盖init方法并在那里执行。
如果我需要在从nib加载后在我的视图对象中做任何事情,我将覆盖awakeFromNib。
#2
Sounds like you're looking for the awakeFromNib function.
听起来你正在寻找awakeFromNib函数。
#3
You can use the - applicationDidFinishLaunching: or - applicationWillFinishLaunching: delegate messages, by implementing one of them in your application delegate/controller, and do whatever initialization you want there.
您可以使用 - applicationDidFinishLaunching:或 - applicationWillFinishLaunching:委托消息,在应用程序委托/控制器中实现其中一个消息,并在那里进行任何初始化。
#1
Cocoa gives you many places to perform tasks before and after objects are loaded from a nib, but it's important to read the documentation carefully to make sure things are happening in the order you expect. Usually I use the following strategy when I'm working on a Cocoa application:
在从笔尖加载对象之前和之后,Cocoa为您提供了许多执行任务的位置,但仔细阅读文档以确保按照您期望的顺序进行操作非常重要。通常我在使用Cocoa应用程序时使用以下策略:
- Where appropriate I implement the +(void)initialize method, which is called before any instances of a class are created. I'll probably set the app's default preferences here, for example.
- In my application controller (app delegate), I implement the applicationDidFinishLaunching: delegate method to load my data file. If this works okay, I then create the window controller(s) and display any windows I want to show at launch.
- In the window/view controllers, I override windowDidLoad: or loadView to perform tasks involving objects loaded from a nib. If I need to create any instance variables that don't involve the nib, I also override the init method and do that there.
- If I need to do anything in my view objects after they're loaded from a nib, I'll override awakeFromNib.
在适当的地方,我实现了+(void)initialize方法,该方法在创建类的任何实例之前调用。例如,我可能会在这里设置应用程序的默认首选项。
在我的应用程序控制器(app delegate)中,我实现了applicationDidFinishLaunching:delegate方法来加载我的数据文件。如果这样可行,我会创建窗口控制器并显示我想在启动时显示的任何窗口。
在窗口/视图控制器中,我覆盖windowDidLoad:或loadView来执行涉及从笔尖加载的对象的任务。如果我需要创建任何不涉及nib的实例变量,我也会覆盖init方法并在那里执行。
如果我需要在从nib加载后在我的视图对象中做任何事情,我将覆盖awakeFromNib。
#2
Sounds like you're looking for the awakeFromNib function.
听起来你正在寻找awakeFromNib函数。
#3
You can use the - applicationDidFinishLaunching: or - applicationWillFinishLaunching: delegate messages, by implementing one of them in your application delegate/controller, and do whatever initialization you want there.
您可以使用 - applicationDidFinishLaunching:或 - applicationWillFinishLaunching:委托消息,在应用程序委托/控制器中实现其中一个消息,并在那里进行任何初始化。