本文介绍了无法使用故事板将窗口连接到IBOutlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问AppDelegate中的窗口.如果我创建一个没有情节提要的新OSX Cocoa项目,则AppDelegate包含这样的窗口变量

I'm trying to access the Window in the AppDelegate. If I make a new OSX Cocoa project without storyboards, then the AppDelegate contains a window variable like so

class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

我可以删除插座和窗户之间的连接,并一遍又一遍地重新连接,一切正常.

I can delete the connection between the outlet and the window and reconnect it over and over again and it works fine.

现在,如果我创建一个带有情节提要的新OSX Cocoa项目,并将window变量添加到AppDelegate中,然后像在没有情节提要的项目上所做的一样,尝试控制将插座拖至窗口,则将不起作用.

Now if I create a new OSX Cocoa project with storyboards and add the window variable to the AppDelegate and try to control drag the outlet to the window the same as I did on the project without story boards it doesn't work.

有什么建议吗?

(OSX 10.11.5 Xcode 7.3.1(和8 beta 6)迅速的2.2(和3))

(OSX 10.11.5 Xcode 7.3.1 (and 8 beta 6) swift 2.2 (and 3))

我知道我可以用代码做到这一点,只是不知道为什么我不能连接插座.

I know I can do it in code just don't know why I can't connect the outlet.

推荐答案

故事板由场景组成,每个场景都可以视为一个单独的xib文件.您无法在位于单独的xib中的对象之间建立连接(动作,出口等),并且-类似地,您无法在位于故事板中的不同场景中的对象之间进行连接.

Storyboards are composed of scenes, and each scene can be thought of as a single xib file. You can't make connections (actions, outlets, etc) between objects residing in separate xibs, and - analogously - you can't make connections between objects residing in separate scenes in a storyboard.

在默认的情节提要板设置中,您的 AppDelegate 对象和window对象位于不同的场景中-因此,为什么不能创建出口:

In the default storyboard setup, your AppDelegate object and the window object are housed in separate scenes - hence why you can't create the outlet:

相比之下,在 xib 应用中,两个对象都驻留在同一个xib文件中,因此可以创建连接 .

By contrast, in a xib app both objects reside in the same xib file so creating a connection is possible.

这篇关于无法使用故事板将窗口连接到IBOutlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 19:51