I am planning to write a custom view that will render some stuff using Quartz. After looking into some sample code in apple site, I decided to subclassing UIView to create my own view:
@interface myView : UIView {
}
@end
Now my question is, what would be the best way t bind my view with the viewCo开发者_运维百科ntroller? While loading from NIB file, we directly assign the view controller file as fileowner type. In case of custom UIView, how can we do this?
When not working with nib files, you use the loadView
method in a UIViewController
to create your view. This comment even appears above loadView
in the template for a new UIViewController
:
// Implement loadView to create a view hierarchy programmatically, without using a nib.
Edit: I may have been too terse here. Basically, then inside your loadView
method, you instantiate your myView (using alloc/init, or whatever), and assign it to self.view
. Let me know if you need example code.
A little confused by your wording. I assume you want to place your custom view in your UIViewController.
You can easily do this both in Interface builder and within your code.
Code:
myView *view = [[myView alloc] initWithRect:CGRectMake([x],[y],[width], [height]];
[self.view addSubView:view];
[view release];
In Interface Builder:
- Select "View" in the Library Objects.
- Drag View unto your UIViewController's view.
- In the inspector click the i in a circle icon ("View Identity")
- Using the class dropdown select the class of your view (myView)
Here's more info: http://developer.apple.com/iphone/library/documentation/DeveloperTools/Conceptual/IB_UserGuide/CustomizingInterfaceBuilder/CustomizingInterfaceBuilder.html#//apple_ref/doc/uid/TP40005344-CH16-SW12
If your viewController already has an Interface Builder .nib file (as when built with the Xcode templates), open it. There's usually a view already in it. You can bring up the inspector for that view, and in the Identity tab of the inspector, change the class of the view controller's view from UIView to your custom view class. Save, rebuild, and your custom class is now bound and will load with that view controller.
If you write and compile your custom view controller (it can be an empty template), it should appear in the inspector to just select as the class for the view controller's view.
精彩评论