0

The setup is as follows:

  • Menubar application with one view controller. The view from the view controller is stored in a NSMenuItem
  • That view controller has one button we'll call 'Settings button'
  • When you click Settings button a NSPopover is opened.
  • The NSPopover has it's content set to SettingsViewController
  • The SettingsViewController has one button.

The button in SettingsViewController does not animate when clicked. It does not respond to any user action. I don't mean that the 'action' is fired, I mean there is not physical response. It's as if the button is disabled even though I have not disabled it.

AppDelegate.swift:

class AppDelegate: NSObject, NSApplicationDelegate {


    let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(NSSquareStatusItemLength)
    let direktMain = DirektMainViewController(nibName:"DirektMainViewController", bundle:nil)

    let mainItem = NSMenuItem()
    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
        statusItem.button!.title = "Dkt"
        statusItem.menu = NSMenu()


        mainItem.view = direktMain!.view;
        statusItem.menu!.addItem(mainItem)
    }
}

SettingsViewController.swift:

class SettingsViewController: NSViewController {

    @IBOutlet weak var quittButton: NSButton?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        print("SettingsViewController. ViewDidLoad")
    }

    @IBAction func quitAction(sender: AnyObject) {
        print("Time to quit!")   
    }        
}

DirektMainViewController.swift:

class DirektMainViewController: NSViewController {

    @IBOutlet weak var settingsButton: NSButton?

    let popoverSettings = NSPopover()
    let settingsViewController = SettingsViewController(nibName:"SettingsViewController", bundle:nil)

    @IBAction func settingsButtonAct(sender: AnyObject) {
        print("Hey: ", __FUNCTION__)
        if !popoverSettings.shown {
            popoverSettings.showRelativeToRect(settingsButton!.bounds, ofView: settingsButton!, preferredEdge: NSRectEdge.MinY)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        print("DirektMainViewController viewDidLoad")
        popoverSettings.contentViewController = settingsViewController
    }
}

Swift Problem

4

0 回答 0