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

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.

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

  • In Xcode 4 I create a new blank document-based Cocoa application
  • 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 FaceViewController for testing purposes it should only display text for now.
  • I open the NSViewController.xib in 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.
  • In MyDocument.xib I also add a Custom View place holder, which should be replaced with my custom FaceView.
  • From this starting point on, everything I tried to include the created View + ViewController on my MyDocument.xib failed and the area where my text should be shown remains empty (just like the background of the underlying window.

    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:

  • The custom View extends from NSView so 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 FaceViewController in 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?
  • 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.


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

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

    [customView addSubview:[FaceViewController view]];
    

    of if you want to replace the view

    [customView replaceSubview:oldView with:[FaceViewController view]];
    
    链接地址: http://www.djcxy.com/p/53612.html

    上一篇: Django是否应该实现DB

    下一篇: 使用笔尖创建自定义视图并在窗口中使用它