简体   繁体   中英

Help on creating a Custom View

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 viewController? 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:

  1. Select "View" in the Library Objects.
  2. Drag View unto your UIViewController's view.
  3. In the inspector click the i in a circle icon ("View Identity")
  4. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM