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

↘锁芯ラ 提交于 2019-12-29 01:28:53

问题


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 set up as modal because if I don't, the user could click the same button again and end up with two copies of the same window.

Is there a way for me to accomplish this without having to restructure the storyboard to embed these view controllers in a separate window controller (which would seem to defeat the purpose of the flexibility the storyboards offer)?


回答1:


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



来源:https://stackoverflow.com/questions/36096257/os-x-storyboards-using-show-segue-without-allowing-duplicate-new-windows-to-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!