0

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

Trombone0904
  • 4,132
  • 8
  • 51
  • 104
  • `NSTableViewController`? And what is the problem? – Willeke Jul 02 '17 at 21:15
  • sry wrong explanation. View Controller with an NSTableView included. My problem can be read in the first post. – Trombone0904 Jul 03 '17 at 05:42
  • If you press a button, the popover appears and when you press the close button the popover disappears without problems? – Willeke Jul 03 '17 at 06:33
  • please download the example - you will see the problem directly – Trombone0904 Jul 03 '17 at 06:38
  • Aha, the popover doesn't close by itself when you press another button. See [OS X storyboards: using “show” segue without allowing duplicate new windows to show?](https://stackoverflow.com/questions/36096257/os-x-storyboards-using-show-segue-without-allowing-duplicate-new-windows-to-s) and [Moving to another view controller in iOS on Button Click](https://stackoverflow.com/a/19573982/4244136). – Willeke Jul 03 '17 at 06:40
  • sry but I think you don't understand the problem. and i don't know how can I explain it better as in post 1 – Trombone0904 Jul 03 '17 at 08:08
  • This is how I read your question: "There's a table view with a button in each cell. The button triggers a popover which can be closed by pressing a close button. There's also a plus button which triggers the same popover.". Which part isn't working? – Willeke Jul 03 '17 at 11:37
  • the button in the cell can't close the popover with the close button. that is the problem – Trombone0904 Jul 03 '17 at 12:37
  • The button in the cell uses the close button to close the popover? I think I know what you mean and I'll write an answer. – Willeke Jul 03 '17 at 15:02

1 Answers1

2

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.

Willeke
  • 14,578
  • 4
  • 19
  • 47
  • i do all steps until step 2. but by step 3 i have a problem. i should connect the cell button of my tableview with the function showPopover? – Trombone0904 Jul 03 '17 at 17:54