关于创建自定义视图的帮助

发布于 2024-09-17 04:54:43 字数 262 浏览 4 评论 0原文

我计划编写一个自定义视图,它将使用 Quartz 渲染一些内容。 在查看了苹果网站中的一些示例代码后,我决定对 UIView 进行子类化以创建自己的视图:

@interface myView : UIView {

}

@end

现在我的问题是,将我的视图与 viewController 绑定的最佳方法是什么? 从 NIB 文件加载时,我们直接将视图控制器文件指定为 fileowner 类型。 如果是自定义UIView,我们该怎么做呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮年慕年 2024-09-24 04:54:43

当不使用 nib 文件时,您可以使用 UIViewController 中的 loadView 方法来创建视图。此注释甚至出现在新 UIViewController 模板中的 loadView 上方:

// Implement loadView to create a view hierarchy programmatically, without using a nib.

编辑:我在这里可能太简洁了。基本上,然后在您的 loadView 方法中,实例化 myView(使用 alloc/init 或其他方法),并将其分配给 self.view。如果您需要示例代码,请告诉我。

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.

情丝乱 2024-09-24 04:54:43

对你的措辞有点困惑。我假设您想将自定义视图放置在 UIViewController 中。

您可以在界面构建器和代码中轻松执行此操作。

代码:

myView *view = [[myView alloc] initWithRect:CGRectMake([x],[y],[width], [height]];
[self.view addSubView:view];
[view release];

在 Interface Builder 中:

  1. 在库对象中选择“View”。
  2. 将 View 拖到 UIViewController 的视图中。
  3. 在检查器中单击圆圈图标中的 i(“View Identity”)
  4. 使用类下拉列表选择视图的类 (myView)

以下是更多信息:http://developer.apple.com/iphone/library /documentation/DeveloperTools/Conceptual/IB_UserGuide/CustomizingInterfaceBuilder/CustomizingInterfaceBuilder.html#//apple_ref/doc/uid/TP40005344-CH16-SW12

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

寄居人 2024-09-24 04:54:43

如果您的 viewController 已有 Interface Builder .nib 文件(如使用 Xcode 模板构建时),请打开它。通常其中已经有一个视图。您可以打开该视图的检查器,然后在检查器的“身份”选项卡中,将视图控制器的视图类从 UIView 更改为您的自定义视图类。保存、重建,您的自定义类现在已绑定并将随该视图控制器一起加载。

如果您编写并编译自定义视图控制器(它可以是空模板),它应该出现在检查器中以仅选择作为视图控制器视图的类。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文