Can't Connect window to IBOutlet with storyboard

倖福魔咒の 提交于 2019-12-10 18:46:49

问题


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.

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.

Any suggestions?

(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.


回答1:


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.

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:

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



来源:https://stackoverflow.com/questions/39141785/cant-connect-window-to-iboutlet-with-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!