我一直在使用 MacRuby,并阅读了 Matt Aimonetti 所著的 MacRuby: The Definitive Guide 一书。
在Movies CoreData 应用程序示例中,我有以下代码:
def add_image(sender)
movie = movies.selectedObjects.lastObject
return unless movie
image_panel = NSOpenPanel.openPanel
image_panel.canChooseDirectories = false
image_panel.canCreateDirectories = false
image_panel.allowsMultipleSelection = false
image_panel.beginSheetModalForWindow(sender.window, completionHandler: Proc.new{|result|
return if (result == NSCancelButton)
path = image_panel.filename
# use a GUID to avoid conflicts
guid = NSProcessInfo.processInfo.globallyUniqueString
# set the destination path in the support folder
dest_path = applicationFilesDirectory.URLByAppendingPathComponent(guid)
dest_path = dest_path.relativePath
error = Pointer.new(:id)
NSFileManager.defaultManager.copyItemAtPath(path, toPath:dest_path, error:error)
NSApplication.sharedApplication.presentError(error[0]) if error[0]
movie.setValue(dest_path, forKey:"imagePath")
})
end
该应用程序加载正常并且运行正常 - 我可以在 CoreData 中创建新电影并删除它们等。但是,当我单击调用此函数的按钮时,它会很好地打开对话框窗口,但是“取消”或“打开文件” " 按钮在这里导致崩溃:
#import <Cocoa/Cocoa.h>
#import <MacRuby/MacRuby.h>
int main(int argc, char *argv[])
{
return macruby_main("rb_main.rb", argc, argv); << Thread 1: Program received signal EXC_BAD_ACCESS
}
任何帮助表示赞赏。我认为它与 BridgeSupport 有关,但嵌入不起作用或我尝试这样做不起作用。无论哪种方式,其他东西似乎很无聊,因为本书提供的示例也崩溃了。
谢谢!
补充说明:
我去 macruby.org 测试了这段代码,它运行良好:
def browse(sender)
# Create the File Open Dialog class.
dialog = NSOpenPanel.openPanel
# Disable the selection of files in the dialog.
dialog.canChooseFiles = false
# Enable the selection of directories in the dialog.
dialog.canChooseDirectories = true
# Disable the selection of multiple items in the dialog.
dialog.allowsMultipleSelection = false
# Display the dialog and process the selected folder
if dialog.runModalForDirectory(nil, file:nil) == NSOKButton
# if we had a allowed for the selection of multiple items
# we would have want to loop through the selection
destination_path.stringValue = dialog.filenames.first
end
end
似乎在 beginSheetModalForWindow 调用中出现了一些问题,或者我错过了一些东西,试图追踪什么。我可以使用上面的代码使用于文件选择的模态对话框工作,但它不是附加到窗口的工作表。