开发者

Opening a new window and waiting for it to close

I have a Mac OS X app written in objetive-c Cocoa. You can see most of the code in this previous question. Essentially you click a button on the main window (the app delegate) and it opens another window where the user can enter information.

In the follow开发者_JAVA百科ing code (that gets called when the user press the button in the app's main window)

- (IBAction)OnLaunch:(id)sender {

    MyClass *controllerWindow = [[MyClass alloc] initWithWindowNibName:@"pop"]; 
    [controllerWindow showWindow:self];

    NSLog(@"this is a log line");
}

The NSLog line gets printer immediately after I called showWindow. Is there any way to wait until controllerWindow is closed to continue with the NSlog?

The reason for this is that the user set's a value on the new window I opened and I need to collect that value on the same OnLaunch so I need to wait.

I know that modal windows are bad form in Mac, but I have no control over this feature.

I've tried with

[NSApp runModalForWindow:[controllerWindow window]];

and then setting the popup window to

[[NSApplication sharedApplication] runModalForWindow:popupwin];

and it works but then the focus never gets passed to the main window anymore Thanks!


If you want the window to be modal for your application, use a sheet: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Sheets/Tasks/UsingCustomSheets.html

However, there is no way to suspend execution of a method while the sheet is displayed, this would be tantamount to blocking the current run loop. You would have to break you code into the begin and end methods as described in the linked documentation.

Here are the steps you need to follow:

  • In TestAppAppDelegate create an NSWindow outlet to hold your sheet and an action to dismiss the sheet
  • Create a nib with an NSWindow as the root object. I think you already have this in "pop". Set the Visible at Launch option to NO (this is very important)
  • Set the file's owner of this nib to TestAppAppDelegate and connect the window to your new outlet, and the close button to your new action
  • In your method to launch the sheet (OnLaunch), use the following code:

(ignore this it's to make the code format properly!)

if(!self.sheet)
  [NSBundle loadNibNamed:@"Sheet" owner:self];
    
[NSApp beginSheet:self.sheet 
   modalForWindow:self.window 
    modalDelegate:self 
   didEndSelector:@selector(didEndSheet:returnCode:contextInfo:) 
      contextInfo:nil];
  • Your close button action should be [NSApp endSheet:self.sheet];
  • Your didEndSheet: method should be [self.sheet orderOut:self];


You can use UIVIew method animateWithDuration:delay:options:animations:completion: to accomplish this.

You said you want the next line to execute once the window is closed, rather than after it is opened. In any case, you may end the OnLaunch method this way:

- (IBAction)OnLaunch:(id)sender {

    MyClass *controllerWindow = [[MyClass alloc] initWithWindowNibName:@"pop"]; 

    [controllerWindow animateWithDuration:someDelay:options: someUIAnimationOption
    animations:^{
        [controllerWindow showWindow:self]; // now you can animate it in the showWindow method
    }
    completion:^{
         [self windowDidFinishShowing];  // or [self windowDidFinishDisappearing]
    }
}

- (void) windowDidFinishShowing {
    NSLog(@"this is a log line");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜