Macos 10.10上,NSPopover中的NSViewController通过swift中NSStatusItem的segues
我已经尝试了几个小时,试图让NSPover加载NSViewController以从NSStatusItem显示,但没有成功,理想情况是在swift中使用segue 我已经设法从DP3中的新NSStatusItem.button实现中正确触发事件,但我无法确定如何通过编程向NSButton添加segue。到目前为止我一直在使用的代码是Macos 10.10上,NSPopover中的NSViewController通过swift中NSStatusItem的segues,macos,swift,Macos,Swift,我已经尝试了几个小时,试图让NSPover加载NSViewController以从NSStatusItem显示,但没有成功,理想情况是在swift中使用segue 我已经设法从DP3中的新NSStatusItem.button实现中正确触发事件,但我无法确定如何通过编程向NSButton添加segue。到目前为止我一直在使用的代码是 class AppDelegate: NSObject, NSApplicationDelegate { var statusItem : NSStatusIte
class AppDelegate: NSObject, NSApplicationDelegate {
var statusItem : NSStatusItem
init() {
statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(CGFloat(-2))
// When the bug is fixed, replace the line above with this line.
//statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(CGFloat(NSSquareStatusItemLength))
}
override func awakeFromNib() {
SetupStatusItem()
}
func SetupStatusItem() {
statusItem.title = nil
var icon = NSImage(named: "Moon_Full.png")
icon.size = NSSize(width: 16, height: 16)
icon.setTemplate(true)
statusItem.button.image = icon
statusItem.button.action = "StatusItemClicked"
}
func StatusItemClicked() {
if (statusItem.button.appearsDisabled)
{
statusItem.button.appearsDisabled = false
println("Hide NSPopover here")
}
else
{
statusItem.button.appearsDisabled = true
println("Show NSPopover here")
}
}
}
这将设置NSStatusItem,它在明暗模式下具有正确的颜色,并记录正确的消息并在活动和非活动状态之间切换
我缺少的第二个问题是如何通过编程方式向statusItem.button添加一个segue,以便它在单击时从NSStatusItem加载我在NSPover中构建的NSViewController
编辑:
我在ViewController上尝试了许多使用按钮的场景
A) 在interface builder中连接segue。。。工作
B) 连接segue并在代码中触发它。。工作正常,但您仍必须在interface builder中建立连接,这样就无法解决当前的问题。(这是10.10版的新API)
C) 尝试在同一情节提要中弹出视图控制器是发生错误的地方
var newView = NSView(frame: sender.frame)
var popupViewController = ViewController(nibName: "PopupViewController", bundle: NSBundle(identifier: "PopupViewController"))
presentViewController(popupViewController,asPopoverRelativeToRect: sender.frame, ofView: newView,preferredEdge: NSRectEdge.max,behavior: NSPopoverBehavior.Transient)
2014-07-17 08:57:55.238 popover[5116:2277209]-[NSNib
initWithNibName:bundle:]无法加载nibName:
捆绑包中的PopupVIewController(null)
(我曾尝试在IB中命名NIB,并按名称调用以及使用null
D) 如果我创建一个单独的XIB,同样的代码也可以使用,但是如果可能的话,我更喜欢使用新的故事板,因为我想使用新的选项卡视图作为容器
我创建了一个示例项目(在Objective-C中,以确保这不是一个swift问题)
提前谢谢你的帮助。别这样。使用
nspover.showRelativeToRect(…)
直接调用popover。我相信如果您将popover的行为设置为.Transient
,它应该在您单击其他位置时自动关闭,但是您需要设置它的delegate
属性以捕获PopoOverridClose
事件并取消高亮显示状态项
编辑:
据我所知,没有办法通过编程在按钮上设置一个segue。当您在故事板中设置一个segue关系时,我相信编译代码,您只需要为您设置一个执行NSStoryboard segue的目标/操作集。所以你不能这么做。在视图控制器中,您可以使用presentViewController(asPopover…
在弹出窗口中快速显示另一个视图控制器。但是在处理NSStatusItem和NSStatusBarButton时,您没有视图控制器,因此必须直接执行该操作。我得到的有效答案是
popover = NSPopover()
popover.contentViewController = NSStoryboard(name: "PopoverStoryboard", bundle: nil).instantiateControllerWithIdentifier("PopoverStoryboardVC") as NSViewController
popover.behavior = NSPopoverBehavior.Transient
简单提示一下:应该以小写字母开头命名方法。大写字母是用来表示类型的。这是我多年的C#开发经历。我也尝试过,但在加载未命名的笔尖时不断遇到错误。(与API更改和新工具支持的beta语言不同)要知道我错了什么和工具生成的错误是一件非常困难的事情。我总是先假定我有错。:-)请粘贴您得到的确切错误。在没有看到症状的情况下很难做出诊断;)
popover = NSPopover()
popover.contentViewController = NSStoryboard(name: "PopoverStoryboard", bundle: nil).instantiateControllerWithIdentifier("PopoverStoryboardVC") as NSViewController
popover.behavior = NSPopoverBehavior.Transient