xcode 使用笔尖创建自定义视图并在窗口中使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6651326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:27:14  来源:igfitidea点击:

Create a custom view using a nib and use it in a window

xcodecocoainterface-buildernibnsviewcontroller

提问by Besi

I have been struggling quite a bit with this problem and I can't seem to figure it out by myself.

我一直在为这个问题苦苦挣扎,我似乎无法自己弄清楚。

This is the situation: I want to have a custom Nib-based view, with its own ViewController. I then want to add this view to a window. Therefore the window should contain my custom view.

情况是这样的:我想要一个自定义的基于 Nib 的视图,它有自己的 ViewController。然后我想将此视图添加到窗口中。因此窗口应该包含我的自定义视图。

So I go about and create a very simple example like this:

所以我开始创建一个非常简单的例子,如下所示:

  1. In Xcode 4 I create a new blank document-based Cocoa application
  2. I create a new Cocoa Class which should extend from NSViewController(which causes the nib to be created along with the .h and .m file. The name of the created .h, .m und .xib file is FaceViewControllerfor testing purposes it should only display text for now.
  3. I open the NSViewController.xibin InterfaceBuilder and add a multi-line text component and place it in the custom view, which is already in the xib file. I make the text span the whole view.
  4. In MyDocument.xibI also add a Custom Viewplace holder, which should be replaced with my custom FaceView.
  1. 在 Xcode 4 中,我创建了一个新的基于空白文档的 Cocoa 应用程序
  2. 我创建了一个新的 Cocoa 类,它应该从NSViewController(这会导致与 .h 和 .m 文件一起创建笔尖。创建的 .h、.m 和 .xib 文件的名称FaceViewController用于测试目的,它应该只显示现在的文字。
  3. NSViewController.xib在 InterfaceBuilder 中打开并添加一个多行文本组件并将其放置在自定义视图中,该视图已经在 xib 文件中。我使文本跨越整个视图。
  4. MyDocument.xib我还添加了一个Custom View占位符,它应该替换为我的自定义 FaceView。

From this starting point on, everything I tried to include the created View + ViewController on my MyDocument.xibfailed and the area where my text should be shown remains empty (just like the background of the underlying window.

从这个起点开始,我尝试将创建的 View + ViewController 包含在MyDocument.xib失败的所有内容中,并且应显示文本的区域仍然是空的(就像底层窗口的背景一样。

So my question really is what I need to do, so that my FaceView gets loaded into the Custom View which I placed on MyDocument.xib. Here are some things which are unclear to me:

所以我的问题真的是我需要做什么,以便我的 FaceView 被加载到我放在MyDocument.xib. 以下是我不清楚的一些事情:

  • The custom View extends from NSViewso I wanted to change this to my FaceView but the view does not exist as a class but is defined in InterfaceBuilder in the xib, do I need to set this to anything other than NSView?
  • Do I have to alloc, init the FaceViewControllerin code or is it enough to drag it into either of my two .xibs?
  • Can I use InterfaceBuilder to place my FaceView or do I have to do this programmatically?
  • 自定义视图扩展自,NSView所以我想将其更改为我的 FaceView,但该视图不作为类存在,而是在 xib 的 InterfaceBuilder 中定义,我是否需要将其设置为除NSView?
  • 我必须分配、初始化FaceViewControllerin 代码还是将它拖到我的两个 .xib 中的任何一个中就足够了?
  • 我可以使用 InterfaceBuilder 放置我的 FaceView 还是必须以编程方式执行此操作?

I really thought that creating a custom view like this would be a piece of cake but it turned out quite the opposite so far, so any help would be greatly appreciated.

我真的认为创建这样的自定义视图将是小菜一碟,但到目前为止结果恰恰相反,因此将不胜感激任何帮助。

采纳答案by iain

You can create your FaceViewController either by adding one to the MyDocument.xib or by creating it with alloc, init.

您可以通过向 MyDocument.xib 添加一个或通过使用 alloc、init 创建它来创建您的 FaceViewController。

To place your FaceView you'll have to do it programmatically, you can use

要放置您的 FaceView,您必须以编程方式进行,您可以使用

[customView addSubview:[FaceViewController view]];

of if you want to replace the view

如果你想替换视图

[customView replaceSubview:oldView with:[FaceViewController view]];