macos - 在 10.10 上,NSPopover 中的 NSViewController 通过从 swift 中的 NSStatusItem 中转出

标签 macos swift

我已经尝试了几个小时,但没有成功,试图让 NSPopover 加载 NSViewController 以从 NSStatusItem 中显示,最好在 swift 中使用 segue。

我已经成功地从 DP3 中的新 NSStatusItem.button 实现中正确触发事件,但我无法弄清楚如何以编程方式向 NSButton 添加 Segue。到目前为止我正在工作的代码是......

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,它在浅色和深色模式下具有正确的颜色,并记录正确的消息并在事件和非事件状态之间切换。

我缺少的 secret 是如何以编程方式向 statusItem.button 添加一个 segue,以便它在单击时执行我在 NSStatusItem 的 NSPopover 中构建的 NSViewController 的加载。

编辑:

我在 ViewController 上使用普通 NSButton 尝试了多种场景

A)在界面生成器中连接一个segue...有效

B)连接一个segue并在代码中触发它..可以工作,但是您仍然必须在界面生成器中进行连接,这样并不能解决手头的问题。 (这是 10.10 中新增的 API)

performSegueWithIdentifier("ShowPop", sender: sender)

C) 尝试在同一 Storyboard 中弹出 View Controller 是发生错误的地方。

    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 initWithNibNamed:bundle:] could not load the nibName: PopupVIewController in bundle (null).

( 我尝试在 IB 中命名 NIB 并按名称调用以及使用 null。

D)如果我创建一个单独的 XIB,则相同的代码可以工作,但我的偏好是如果可能的话使用新的 Storyboard,因为我想使用新的选项卡 View 作为容器......

我创建了一个示例项目(在 Objective-C 中以确保这不是一个快速问题)

Sample ObjectiveC project

提前感谢您的帮助。

最佳答案

我得到的有效答案是......

    popover = NSPopover()
    popover.contentViewController = NSStoryboard(name: "PopoverStoryboard", bundle: nil).instantiateControllerWithIdentifier("PopoverStoryboardVC") as NSViewController
    popover.behavior = NSPopoverBehavior.Transient

关于macos - 在 10.10 上,NSPopover 中的 NSViewController 通过从 swift 中的 NSStatusItem 中转出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24768495/

相关文章:

c++ - Xcode中发生的一些奇怪的事情

XCode 4 无法打开旧项目

macos - 如何检测 Mac 上的屏幕旋转?

swift - 使用 Swift 代码访问 OS X 上的媒体库

swift - NSImageView 的连续旋转(因此它看起来是动画的)

linux - 使用 SED 替换长字符串 - 但在正则表达式中得到未终止的替换

objective-c - Cocoa NSView 改变自动调整属性

swift - 使用 arc4random_uniform 返回整数和非整数 double

ios - 如何通过在 CollectionView 中选择一个单元格来更改图像

ios - 导航栏问题, swift