在不同的故事板上打开一个新窗口

时间:2016-08-11 14:44:25

标签: swift macos cocoa storyboard selector

我正在开发一个带有故事板的OS X应用程序。我在带有自定义ViewController的匿名窗口上使用入口点获得Preview.storyboard。在AppDelegate类中,我得到以下函数。

func newPreviewWindow(sender: AnyObject) {
    let storyboard = NSStoryboard.init(name: "Preview", bundle: nil)
    let initialController = storyboard.instantiateInitialController()
    initialController!.showWindow(nil)
    initialController!.makeKeyAndOrderFront(nil)
}

运行代码时,窗口显示,但我得到以下异常:

2016-08-11 10:27:12.434 MyApp[1090:290439] -[NSWindowController makeKeyAndOrderFront:]: unrecognized selector sent to instance 0x60000008c350
2016-08-11 10:27:12.434 MyApp[1090:290439] -[NSWindowController makeKeyAndOrderFront:]: unrecognized selector sent to instance 0x60000008c350
2016-08-11 10:27:12.440 MyApp[1090:290439] (
    0   CoreFoundation             0x00007fff926284f2 __exceptionPreprocess + 178
    1   libobjc.A.dylib            0x00007fff97c0ef7e objc_exception_throw + 48
    2   CoreFoundation             0x00007fff926921ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation             0x00007fff92598571 ___forwarding___ + 1009
    4   CoreFoundation             0x00007fff925980f8 _CF_forwarding_prep_0 + 120
    5   MyApp                      0x0000000100005617 
...

基于异常消息并在Google和StackOverflow上搜索,我尝试以这种方式向makeKeyAndOrderFront函数发送选择器:

func newPreviewWindow(sender: AnyObject) {
    let storyboard = NSStoryboard.init(name: "Preview", bundle: nil)
    let initialController = storyboard.instantiateInitialController()
    let selector = #selector(AppDelegate.newPreviewWindow(_:))
    initialController!.showWindow(nil)
    initialController!.makeKeyAndOrderFront(selector) // [A]
}

但是我在[A]行上得到以下编译错误:Cannot call value of non-function type '((AnyObject?) -> Void)!'

打开新窗口或传递选择器的正确方法如何?

谢谢! :)

1 个答案:

答案 0 :(得分:1)

df$gap <- c(0, (diff(df$date) > 1)*1) # identify gap between dates larger than 1 df$group <- cumsum(df$gap) + 1 # cumulative sum of 'gap' variable 导致问题,因为initialController!.makeKeyAndOrderFront(nil)不是makeKeyAndOrderFront:方法 - 它属于NSWindowController(因此无法识别的选择器错误) 。将初始控制器转换为NSWindow子类,然后通过控制器的NSWindowController属性将窗口置于前面:

window