iPhone: Accessing view objects from a ViewController in Code
Let's say I have a ViewController
that needs access to a view. In the class file of the ViewController
I am doing
IBOutlet ViewA *someview;
And in Interface Builder, I drag and drop a UIView
in my document and set the class to ViewA
.
I am missing how "stuff" is instantiated when you connect through IB. Is ViewA
automatically allocated when the .xib files are unarchived? What if I don't want t开发者_开发问答o use IB?
If you don't want to use IB, instead of putting that IBOutlet there, you just eliminate it, leaving just
ViewA *someview;
and then somewhere in your code, when you need the view, you do a
someView = [[UIView alloc] initWithFrame: rect];
I prefer IB, others prefer programmatically creating views. I like how I can position all the views subViews, including UIButtons, UILabels, UITableViews, other UIViews, etc. without having to use coordinates to do so. YMMV.
As to when things get instantiated, when using a XIB, your app will probably lazily load the view controller, and once it is loaded, it will load your view. What actually loads your view, is when you first access the variable someView. While there is an outlet connection, the view and its subViews are not loaded till you access someView, in any manner, for example if you just do a:
if (someview) {
// the view is loaded now
}
精彩评论