How to create 'sheet' segue from a Menu Item

3.2k Views Asked by At

I have a Swift Cocoa application that segues a view from a menu item. When i create the segue from the interface builder, the options are: show, modal and custom.

However when i create the segue from a button, more options appear: sheet, popover, show, custom, modal

I need to create a sheet segue from that menu item. Am i missing something? or just these extra options are disabled from a menu item

5

There are 5 best solutions below

2
On BEST ANSWER

As others have said, a menu item can't perform a segue that relies on any attachment to a window. Unfortunately there are cases where you DO want to trigger a "sheet" segue from a menu item. As an example, Xcode itself does this. The + at the far bottom left of an Xcode window will open a three-item menu, all of which perform a "sheet" segue.

One option is to define the segue between the two storyboard scenes, give the segue an Identifier, and then perform the segue programmatically.

  1. click on your first scene, and look for the View Controller icon in the scene's header. It's a blue circle containing a white square.

  2. control-drag from that View Controller icon over onto the second scene (the one you want to show in the sheet). A segue should be created between the two scenes. This segue will offer the full range of segue kinds, including "Sheet".

  3. select the new segue and show the attributes inspector. Give the segue an Identifier e.g. "ShowEditView"

  4. Trigger the segue manually. As a simple test, drag a button onto scene one, and connect it up to your scene one view controller:

    @IBAction func wasClicked(sender: AnyObject) {
        print("Test button was clicked.")
        self.performSegueWithIdentifier("ShowEditView", sender: self)
    }
    
5
On

You are not missing anything. The way I work around this is by selecting one of the segue options and then opening the changed storyboard with the change editor to find the exact spot that was changed for the segue. When opened as XML this is easy to see. I then edit the segue manually to "sheet". Save the storyboard and everything works fine.

1
On

Storyboard only solution

  1. Add an NSStackView in the main view. Mark it hidden from Attributes Inspector
  2. Add an NSButton to this hidden NSStackView
  3. Control drag from the NSMenuItem to the NSButton(from Step 2)
  4. On drag end a menu will appear, select performClick:. This will simulate a mouse click on the NSButton
  5. Control drag from NSButton to the view and now sheet option can be seen in the menu.
  6. Done!!

Note: Add an NSButton to the hidden NSStackView for every view that is to be shown as a Sheet from NSMenu.

1
On

Thanks for the tip.

However i needed the segue, since it was related to an "import" option from the application

Also after some research i found that a "sheet" segue needs a window to be attached to, and a menu is not related to any window. Thats why the option appeared when i tried to create the segue from the button, since the button is inside a view, also the view inside a window.

The solution is related to what Frizlab said, the segue must be attached to a view/window that can display it, then when the menu is clicked just perform that segue

0
On

Sheet segue can’t be enabled from a menu item because it wouldn’t make any sense. A segue represents a way to make something appear from a given UI element to another one.

You simply cannot make a window or anything else appear as a sheet from a menu item; it would not make any sense.

If the sheet can appear from only one view/window, make a sheet segue from this view/window, then call performSegue: in your menu item action with the name of your segue. Else, you should not use a segue and make the sheet appear via some code.

Segues are particularly useful on iOS, where a transition from one view to another are usually triggered from one place only. On Mac OS though, there is much more liberty and possibilities. Segues are not as useful.