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

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
user1660675
  • 149
  • 3
  • 10
  • I would say that you have 2 SettingsViewController going around and you set target/action on the incorrect one. Make a new IBOUtlet to settingsviewcontroller and set it to be popover's contentVC. To verify if you have 2 object add a breakpoint in awakeFromNib – Marek H Oct 04 '15 at 11:48
  • What I mean to say is that when I click down on the mouse the check box doesn't animate. The target action would work (I believe) if the controls physically responded to my clicks. It *seems* like the controls are disabled but they are not because they aren't greyed out. – user1660675 Oct 04 '15 at 14:47
  • Could you please update your question. It is not clear what you are asking for (include screenshot and demo project). – Marek H Oct 04 '15 at 16:35
  • Made the changes. It's the quit button in the image that does not respond – user1660675 Oct 04 '15 at 18:06
  • Update: I changed from showing the DirektMainViewController inside of a NSMenuItem to showing it's view inside of a NSPopover and the second NSPopover *does* respond. I do not know why. – user1660675 Oct 04 '15 at 18:34

0 Answers0