xcode 快速从xib添加OS X addsubview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27228362/
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-15 06:17:20  来源:igfitidea点击:

OS X addsubview from xib in swift

xcodemacoscocoaswiftnsviewcontroller

提问by Swiftly85

I'm trying to add a new sub view form a nib using swift for OS X.

我正在尝试使用 swift for OS X 从笔尖添加一个新的子视图。

So far i've:

到目前为止,我已经:

  • created a new "Cocoa Application"
  • added a new "Cocoa Class" called "TestSubView" as a subclass of NSViewController with a XIB file
  • 创建了一个新的“可可应用程序”
  • 添加了一个名为“TestSubView”的新“Cocoa Class”作为带有 XIB 文件的 NSViewController 的子类

I want to add this subview to my main view when the application loads.

我想在应用程序加载时将此子视图添加到我的主视图中。

in my ViewController ( the ViewController for the main window ) i have.

在我的 ViewController (主窗口的 ViewController )中,我有。

import Cocoa

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let newSubView = TestSubView();
        self.view.addSubview(newSubView.view);

    }

    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

But i'm getting the following error

但我收到以下错误

Failed to set (contentViewController) user defined inspected property on (NSWindow): 
-[NSNib initWithNibNamed:bundle:] could not load the nibName: temp.TestSubView in bundle (null).

I realise i will need to size and position this subview but I can't seem to get to that point.

我意识到我需要调整这个子视图的大小和位置,但我似乎无法做到这一点。

I've spent the better part of a day trying to figure this one out so any help would be greatly appreciated.

我花了一天的大部分时间试图解决这个问题,所以任何帮助都将不胜感激。

回答by Swiftly85

I finally got this thing to work. My new code looks like

我终于让这件事起作用了。我的新代码看起来像

import Cocoa

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let subview = TestSubView(nibName: "TestSubView", bundle: nil)!
        self.view.addSubview(subview.view)
    }

    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }

}

Found with the help of the docs& this answer

在文档这个答案的帮助下找到

It was suggested that if the nib name and the class name are the same you shouldn't need to declare nibname:(as i'd tried to do originally) but the docs didn't mention this - explains why it didn't work!

有人建议,如果笔尖名称和类名称相同,则您不需要声明 nibname:(正如我最初尝试做的那样),但文档没有提到这一点 - 解释了为什么它不起作用!

For prosperity, this worked for me with Xcode 6.1.1 on OS X Yosemite (10.10.1)

为了繁荣,这对我在 OS X Yosemite (10.10.1) 上的 Xcode 6.1.1 有用

回答by Mundi

A nib is really nothing but an XML file with view information in it. You have to get it from the application bundle and get one of the views contained in it explicitly. You are perhaps confounding views and view controllers (your attempt to extract viewfrom newSubViewsuggests that).

笔尖实际上只是一个包含视图信息的 XML 文件。您必须从应用程序包中获取它并显式获取其中包含的视图之一。您也许混杂的观点和看法控制器(您尝试提取viewnewSubView提示)。

Try this:

尝试这个:

let subview = NSBundle.mainBundle().loadNibNamed("TestSubView", 
   owner:self, options:nil)![0]! // maybe no final unwrapping "!" in Swift 3
self.view.addSubview(subview)

Make sure the xib is really called the name you are using and contains at a least one view (otherwise the two unwrapping !above will crash your app).

确保 xib 确实被称为您正在使用的名称,并且至少包含一个视图(否则!上面的两个展开会使您的应用程序崩溃)。