-1

Xcode 9.2, macOS 10.12.6, Swift 4. I don't really know what I'm doing, so please explain what to do in detail.

I am trying to make it so that the first window is closed when the second one is opened. The buttonCONTINUE makes the second window open, via show segue. img1 I followed the Control+Click and drag as explained here, and tried to make the CONTINUE button close the first window when closed in two different ways; with self.view.window?.close() and with setting the key equivalent to CMD+W.img2

I've tried the solution suggested here, but that did not solve my problem.

Edit:


I have two windows, one each goes to the other one. Here is the code:

    override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
        super.prepare(for: segue, sender: sender)

        if segue.identifier!.rawValue == "SegueToWIR" {
            view.window?.close()
        }
        //neither if or else if make this work
        if segue.identifier!.rawValue == "SegueToWarning" {
            view.window?.close()
        }
    }

The second if statement doesn't cause an error, but doesn't do anything.

Sam
  • 157
  • 1
  • 10
  • 1
    Possible duplicate of [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) – Chris Zielinski Sep 07 '19 at 04:42
  • @ChrisZielinski That solution didn't work, thanks for suggesting it though – Sam Sep 07 '19 at 19:34

1 Answers1

1

Unlike UIKit's UIControl, AppKit only permits a single target-action per NSControl instance. You are attempting to use two:

  1. The show segue.
  2. The action method connection you created using the control + click & drag method.

The segue takes precedence. You can verify this by setting a breakpoint at CONTINUE(_:). You'll find that the action method never gets called!

So scrap the action method &, alternatively, use:

Note: The below implementation is valid for Swift 4.2+.

override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)

    // Note: For Swift 4, replace `segue.identifier` with `segue.identifier?.rawValue`.
    if segue.identifier == "my-segue-identifier" {
        view.window?.close()
    }
}
Chris Zielinski
  • 1,231
  • 1
  • 9
  • 11
  • Xcode changes `if segue.identifier` to `if segue.identifier!.rawValue`, which works, so would you mind editing your answer to reflect this? – Sam Sep 09 '19 at 17:11
  • Apparently I can't use this twice, I'm told `'prepare(for:sender:)' has already been overridden`. Is there a way around this? – Sam Sep 09 '19 at 19:03
  • Also, adding another `if segue.identifier!.rawValue == "SegueName" { view.window?.close()` after the existing one doesn't cause the window to close – Sam Sep 09 '19 at 19:11
  • Well you must’ve overridden it somewhere... You can’t have two identical method declarations within the same scope. & I don’t understand your other comment. – Chris Zielinski Sep 09 '19 at 20:26
  • I'll add it to my question – Sam Sep 09 '19 at 20:29
  • If you set a breakpoint inside the second `if` statement, does it ever halt? Is the other segue also a _show_ segue? – Chris Zielinski Sep 09 '19 at 22:00