开发者

should I be creating my objects manually or with InterfaceBuilder?

开发者 https://www.devze.com 2023-02-07 19:53 出处:网络
I am relatively new to Cocoa/Xcode and am not sure if I am structuring my application in the most \"correct\" way possible. I have a number of objects which are active the while time that the applicat

I am relatively new to Cocoa/Xcode and am not sure if I am structuring my application in the most "correct" way possible. I have a number of objects which are active the while time that the application is running. Should I be creating these objects manually or with Interface Builder?

The type of objects I am referring to:

  • "downloader" which is responsible for downloading files to disk
  • user interface updater which is responsible for updating the user interface to show the results of the downloaded file

Sh开发者_如何学编程ould I create these objects in IB and set up the connections between them with code?


It's really a matter of personal preference.

In my opinion IB only really good at laying out views, so I tend to only use IB for my view and my view controller, and I create everything else in code in the view controller's viewDidLoad or init method.

In your example, connecting the "downloader" object directly to the interface seems like an MVC violation, so I would keep the downloader out of my xib.

The "interface updater" would be connected tightly to the interface, so it could be in the nib, although unless I had a good reason not to I would probably just put that code into my view controller class.

If you are creating things in code, note that viewDidLoad/viewDidUnload can potentially be called a number of times, as the os loads and un-loads your views when they are not visible to save memory. So only put transient objects there... things that must exist for the life of the view controller should be created in the init/dealloc methods. Part of why I like to do most of my object creation in code, is the finer level of control you have over memory.


I typically build as much as I can in IB, and then switch to code when I run into the limitations of IB. It sounds like you should be able to create the UI you described in interface builder.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号