Reputation: 449
I am learning about Interface Builder and I saw code like this (self
here refers to a ViewController):
[self presentViewController:anotherViewController animated:YES completion:nil];
I also notice that when I create a subclass of UIViewController
, Xcode offers the option to create the associated xib file as well.
So I assume that this controller knows what its associated view is (by initWithNibName:
). And I also assume that the view knows which controller it is associated to as it is shown as the file's owner.
Here is my question:
From my understanding, a UIView
and its associated controller always go together. Right now I am trying to programmatically create a paged scroll view (horizontal scrolling), where each page is a customized collection view. So I can create the view just fine. But I need some customized control in each collection view. However, I don't have access to the collection view's controllers as I never created them in my code.
How can I create a custom view and have the view controller of it? Thanks!
Upvotes: 0
Views: 684
Reputation: 62072
A UIView
is not always associated with a UIViewController
.
A UIViewController
is always (or should be) associated with a UIView
, although a single view controller does not always have to be associated with the same exact view.
In fact, UIViewController
's have a view
property, which represents the view they are controlling, and this view is not readonly--it can be changed at runtime.
If you're creating a view controller on the storyboard, or creating a view controller with an associated .xib
file, you can create outlets for any elements you've drag-and-dropped onto the view controller. This is done by opening the assistant editor, or double-clicking the companion file to open it in a new window, and Ctrl+Dragging from the UI element to the class's @interface
section (either in the .h
or .m
file, but in almost all cases .m
will be most appropriate).
In addition, when we're talking about UITableView
, UICollectionView
, UITextField
, and other UI elements that have delegate and/or datasource properties, these can also be hooked up via the interface builder.
In addition to this, if you give your UI elements in the interface builder unique tags, you can get a reference to them using the viewWithTag:
method on the view controller's view. This will give you a reference to the view matching the tag sent for the argument.
Finally, if you've not created the element in the interface building and want to create it completely from code, a good tutorial might be what you need. The gist of it is this though... instantiate and set up the view. Once that view is ready, add it to the view controller's view by using the addSubview:
method.
At the end of the day, no matter what method you do of hooking views up, you really need a @property
in your @interface
section so that you can maintain a reference to the view. And with collectionviews/tableviews, it's also important that you set their delegate/datasource properties.
Upvotes: 1