0

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 to use IB?

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.