以编程方式从一个文件设置另一个文件中的值[Swift]

以编程方式从一个文件设置另一个文件中的值[Swift],swift,nsviewcontroller,Swift,Nsviewcontroller,与:不一样,对我的处境没有帮助 当我按下一个文件中的按钮时(NSViewController) 我希望此文件显示bookName=“hello”的结果 我缺少什么?您需要将popVC强制转换为PopoverViewController,以便设置bookName属性,因为NSViewController没有属性bookName: var popVC = NSStoryboard(name: "Main", bundle: nil)?.instantiateControllerWithId

与:不一样,对我的处境没有帮助

当我按下一个文件中的按钮时(NSViewController)

我希望此文件显示bookName=“hello”的结果


我缺少什么?

您需要将popVC强制转换为
PopoverViewController
,以便设置
bookName
属性,因为
NSViewController
没有属性
bookName

var popVC = NSStoryboard(name: "Main", 
    bundle: nil)?.instantiateControllerWithIdentifier("PopoverViewController") as? PopoverViewController
然后,您将需要显示刚才使用
presentViewController(\uu:animated:completion:)


此外,在您的
PopoverViewController
类中,您应该为
bookName
使用
var
,因为它是不变的。

您也可以使用singleton:)不要使用singleton我没有意识到我也需要使用
presentViewController
。非常感谢。
class PopoverViewController: NSViewController {

    let bookName: String = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        println(bookName)
    }
}
var popVC = NSStoryboard(name: "Main", 
    bundle: nil)?.instantiateControllerWithIdentifier("PopoverViewController") as? PopoverViewController