OS X storyboards: using “show” segue without allowing duplicate new windows to show?

前端 未结 1 1521
轻奢々
轻奢々 2020-12-11 11:39

Right now I have an OS X storyboard app that has a main window, and a button on it that triggers a \"show\" segue for another view controller. Right now I\'ve got the segue

相关标签:
1条回答
  • 2020-12-11 12:18

    Edit: While the answer below does work, it is definitely not the best way. In your storyboard, select the view controller for the destination view then go to the attributes inspector and change Presentation from Multiple to Single. That's it, no code required.


    Not sure this is the best way but in the NSViewController that is pushing the segue, you could add a property for the destination NSViewController and, in your prepareForSegue:sender: method, assign the destination view controller. Finally, in the shouldPerformSegueWithIdentifier:sender: method, check to see if the destination view controller is assigned, and, if so, bring its window to the front and return NO meaning don't perform the segue, otherwise, return YES. Here's a quick example (to be included in the NSViewController with the button to initiate the segue):

    @interface ViewController ()
    @property (weak) NSViewController *pushedViewController;
    @end
    
    @implementation ViewController
    
    - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
        if (self.pushedViewController) {
            [self.pushedViewController.view.window makeKeyAndOrderFront:self];
            return NO;
        }
        return YES;
    }
    
    - (void)prepareForSegue:(NSStoryboardSegue *)segue sender:(id)sender {
        self.pushedViewController = segue.destinationController;
    }
    
    @end
    

    When you close the window containing the destination view controller, this will set the pushedViewController property of the original view controller to nil so the segue will perform if the window is not already opened. Again, there may be a better way to do this. Hope this helps.

    Jon

    0 讨论(0)
提交回复
热议问题