Mic*_*sen 7 macos xcode cocoa swift
我使用标准的Xcode Swift模板(使用StoryBoards)创建了一个新的OS X Cocoa应用程序.
我已经在AppDelegate.swift中实现了一个IBAction来处理用户何时从"文件"菜单中选择"打开...".如果所选文件是有效的图像文件,我创建一个NSImage,然后我想在ViewController的视图中显示.
@IBAction func openFile(sender: NSMenuItem) {
var openPanel = NSOpenPanel()
openPanel.beginWithCompletionHandler { (result :Int) -> Void in
if result == NSFileHandlingPanelOKButton {
if let imageURL = openPanel.URL {
let image = NSImage(contentsOfURL: imageURL)
// PRESENT image IN THE VIEW CONTROLLER
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我没有看到任何方法从AppDelegate连接到ViewController.我只是设法找到了我应该看看self.window的建议!在AppDelegate中,但AppDelegate中没有窗口这样的东西.
谢谢Michael Knudsen
blu*_*ome 13
似乎AppDelegate只能连接到故事板中Application Scene中的对象.如果要获取ViewController,请从故事板中实例化它.
样品:
@IBAction func menuAction(sender: AnyObject) {
if let storyboard = NSStoryboard(name: "Main", bundle: nil) {
let controller = storyboard.instantiateControllerWithIdentifier("VC1") as NSViewController
if let window = NSApplication.sharedApplication().mainWindow {
window.contentViewController = controller // just swap
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8961 次 |
| 最近记录: |