Search code examples
macoscocoaswift3nspopover

dismiss nspopover doesn't work


i have a big problem, which i can't solve since a week. My Situation:

I have an ViewController with a NSTableview and custom cells. in each sell is a nsbutton. if you pressed a button, a nspopover will appear. but you can close it with the "close"-button, which is assigned to the dismiss-function. i you pressed the plus button (out of the tableview) the same popover will appear and can close with the "close"-button without problems.

what do i wrong? i attached the example project via google drive. thanks for your help:

Download: https://drive.google.com/open?id=0B8PBtMQt9GdORUxQRXRISWR5dWs


Solution

  • dismissViewController doesn't work if the view controller doesn't have a presenting view controller (I don't know why). Starting from the downloaded project, make the following changes:

    1. Move the showPopover action from CustomCell to TableViewController. Change the type of sender to NSButton.

    2. Present the view controller instead of showing the popover.

      @IBAction func showPopover(_ sender: NSButton) {
          let vcPopover = NSStoryboard(name: "Main", bundle: nil).instantiateController(withIdentifier: "vcPopover") as! NSViewController
          self.presentViewController(vcPopover, asPopoverRelativeTo : sender.bounds, of : sender, preferredEdge: .maxX, behavior: .transient)
      }
      
    3. Connect the action of the button in the table view to the Table View Controller and action showPopover.