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 following 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");
}
Related
I need to open a modal window, which can open another modal window (or sheet). First window works fine, but second behaves strange.
This is how I open first window:
RegisterDialog * registerDialog=[[RegisterDialog alloc] initWithWindowNibName:#"RegisterDialogMac"];
NSWindow* window = [registerDialog window];
[NSApp runModalForWindow:window];
This window behaves properly and responds to any buttons, I just added a bit of code to stop modal event loop after pressing on a red button:
- (void)windowWillClose:(NSNotification *)notification {
[[NSApplication sharedApplication] stopModal];
}
Ok. Now I am opening the second dialog window if user press the "Register" button:
-(IBAction)registerPressed:(id)sender {
RegistrationDialogMac* registrationDialog=[[RegistrationDialogMac alloc] initWithWindowNibName:#"RegistrationDialogMac"];
NSWindow* window = [registrationDialog window];
[NSApp runModalForWindow:window];
}
This second dialog window works fine too, but is closed only if to press red button.
This is how "Cancel" button is processed:
-(IBAction)cancelPressed:(id)sender {
[NSApp stopModal];
[[self window] close];
}
After pressing it modal event loop is stopped, first dialog window becomes active, but second window still remains here. It is closed only if I press the red button. This is strange, as the first modal window is closed properly by the same code.
Ok. I tried to go different route and run the second dialog window as a sheet:
-(IBAction)registerPressed:(id)sender {
RegistrationDialogMac* registrationDialog=[[RegistrationDialogMac alloc] initWithWindowNibName:#"RegistrationDialogMac"];
NSWindow* window = [registrationDialog window];
[self.window beginSheet: window
completionHandler:^(NSModalResponse returnCode) { }
];
}
(I turned off "Visible at launch" to correctly draw this sheet attached to the first dialog window)
This way the second dialog window is drawn, but ignores all button presses. Graphically buttons are pressed, but aren't processed. This code:
-(IBAction)cancelPressed:(id)sender {
NSLog(#"!");
[[self window] close];
//[NSApp endSheet:[self window]]; // may be this would be correct way to close sheet? Don't know as this method isn't run anyway
}
isn't executed at all, there is no a single "!" character in the log.
So I am doing something wrong. I actually almost have no any Mac coding experience, only Windows.
Found that [self window] of the second dialog was nil, so it couldn't respond to close. Checked the .xib - and, yes, I forgot to connect the window outlet to the window itself. Fixed, and the window is closed correctly now.
This doesn't fix the problem of unresponding buttons when this second dialog is drawn as Sheet though.
I am working on my first native mac app after playing with iOS for a while.
I am attempting to launch a window from a menu item, but I suspect I am doing it wrong. Any IBAction I connect to buttons on this new window returns an error.
Here is what I am doing. From a menu item I launch this:
-(IBAction)displaySAInput:(id)sender{
NSLog(#"Input selected.");
inputSAViewController = [[NSWindowController alloc] initWithWindowNibName:#"InputViewController"];
[inputSAViewController showWindow:self];
This launches the InputViewController nib that is owned by the InputViewController class. I set the InputViewController class to inherit from NSWindowController.
On InputViewController.m I have tested IBActions such as:
-(IBAction)testButton:(id)sender{
NSLog(#"Data recalled?");
}
I connect this IBAction to a button through the Interface Builder. All looks okay.
When I build and open the InputViewController window I receive this error in the console before clicking anything:
Could not connect the action testButton: to target of class NSWindowController
I have searched extensively but my ignorance prevents me from connecting the dots. This thread based on a similar error with NSApplication looks promising, but I don't quite understand what I'd need to make the connections happen related to the NSWindowController error.
This should be simple. What am I missing?
Your code:
-(IBAction)displaySAInput:(id)sender{
NSLog(#"Input selected.");
inputSAViewController = [[NSWindowController alloc]
initWithWindowNibName:#"InputViewController"];
[inputSAViewController showWindow:self];
}
Notice you are alloc/initing a generic instance of NSWindowController, not your custom subclass where you've implemented the testButton: method. I assume you'd likely want that changed to:
-(IBAction)displaySAInput:(id)sender{
NSLog(#"Input selected.");
inputSAViewController = [[InputViewController alloc]
initWithWindowNibName:#"InputViewController"];
[inputSAViewController showWindow:self];
}
Just load the NSWindow before hand and make it invisible and when the IBAction is called, just do the following:
[myWindow setIsVisible:YES];
[myWindow setLevel:NSFloatingWindowLevel];
Yay!
I'm writing a multi-document application using Cocoa. The user must enter a password when opening a document. After a certain amount of time without activities on a document the user is once again required to enter the password.
Right now I'm using NSAplication's beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo: to show the password prompt in a custom sheet. While it works it has the unfortunate side-effect of the window being brought to front and given focus even if another document is being worked on at the time. It is only problematic if my application is in front.
Is there a way to prevent opening a sheet from snatching focus if its parent window is not active?
There isn't a simple way. The hacky way would be to make a subclass of NSWindow for both the document's window and the sheet's window, and in that class, override both orderFront: and makeKeyWindow to do nothing during the time you call beginSheet. For example,
In the NSWindow subclass:
-(void)awakeFromNib
{
hack = NO;
}
-(void)hackOnHackOff:(BOOL)foo
{
hack = foo;
}
- (void)orderFront:(id)sender
{
if (!hack)
[super orderFront:sender];
}
- (void)makeKeyWindow
{
if (!hack)
[super makeKeyWindow];
}
And then your beginSheet call would look like:
-(void)sheet
{
SpecialSheetWindow* documentWindow = [self windowForSheet];
[documentWindow hackOnHackOff:YES];
[sheetWindow hackOnHackOff:YES];
[[NSApplication sharedApplication] beginSheet:sheetWindow
modalForWindow:documentWindow
modalDelegate:self didEndSelector:#selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:nil];
[documentWindow hackOnHackOff:NO];
[sheetWindow hackOnHackOff:NO];
}
I'm trying to display a modal dialog on top of my app but it's blocking my main app window when it closes. Here's my code:
TutorialWindowController* pTutorialController = [[TutorialWindowController alloc] initWithWindowNibName:#"TutorialWindow"];
NSWindow* pTutorialWindow = [pTutorialController window];
DDLogInfo(#"Tutorial window opening...");
[NSApp runModalForWindow: pTutorialWindow];
DDLogInfo(#"Tutorial window closed!"); // CODE NEVER GETS HERE
[NSApp endSheet: pTutorialWindow];
[pTutorialWindow orderOut: self];
In the modal dialog, my Close button runs this:
- (IBAction)closeButtonPressed:(id)sender {
[NSApp stopModal];
}
The modal dialog displays fine. However, when I click the Close button, the dialog disappears and my app's main window isn't responsive. I hear the bonk every time I try clicking. I'm pretty sure this is because the code never continues after runModalForWindow. Same thing happens if I close the modal dialo using the red X.
What am I doing wrong?
After ordering out the tutorial window, try doing a
[window makeKeyAndOrderFront:self];
on your main window.
You should call [pTutorialWindow orderOut:nil] first.
Not sure about the closeButtonPressed handler. But try adding to the delegate:
- (void) windowWillClose:(NSNotification *)notification
{
// ...
// In there, you should verify that you are calling:
[NSApp stopModal]
}
Adding the stopModal call solved the issue for me.
Verify that the Window delegate in the Interface Editor's Connection Inspector is connected to the File's Owner.
I had several modal dialogues working correctly except for one, and the missing connection was the only difference. Making the connection fixed the problem.
I'm in the midst of debugging an extremely unusual problem, and I was wondering if anybody might have any insight into what might be going wrong:
In a controller class from a NIB, I take an NSPanel from that same NIB, and then show it app modally on a NSWindow (that was created by hand in code):
[[NSApplication sharedApplication] beginSheet: myPanel
modalForWindow: window
modalDelegate: self
didEndSelector: #selector(sheetDidEnd:returnCode:contextInfo:)
contextInfo: nil];
[[NSApplication sharedApplication] runModalForWindow: myPanel];
Now, when the "finish" button on that sheet is clicked, I run some code to disable some buttons and fire off a thread to make sure the user input is valid (I have to validate with a remote service). This thread is fired from a separate validator object I create:
// controller calls:
[validator validateCreds: creds
notify: #selector(validationComplete:)
onObject: self];
// validator object
validateInfo: (NSDictionary *)parms
notify: (SEL)notifySelector
onObject: (id)notifyObject
{
// build up data with parms and notify info
[[NSThread detachNewThreadSelector: #selector(remotevalidate:)
toTarget: self withObject: data];
}
Next, when the validation is finished, the validator notifies my controller object:
[notifyObject performSelectorOnMainThread: notifySelector
withObject: results waitUntilDone: NO];
And then my controller object, in the method that the validator object calls, kills the dialog:
- (void)validationComplete: (id)data
{
[[NSApplication sharedApplication] stopModal];
[createTwitterPanel orderOut: nil];
[[NSApplication sharedApplication] endSheet: createTwitterPanel
returnCode: NSOKButton];
}
- (void)sheetDidEnd:(NSWindow *)sheet
returnCode:(int)returnCode
contextInfo:(void *)contextInfo
{
m_returnCode = returnCode;
}
My problem: Although the panel is closed / disappears, the top NSApp runModalForWindow: does not exit until some system event is sent to the window that was showing the dialog. Trying to move, resize, or do anything to the window, or otherwise switching away from the application suddenly causes the method to exit and execution to continue. No amount of waiting seems to help, otherwise, however.
I have verified that all methods being invoked on the controller class are all being invoked on the main app thread.
An even more interesting clue is that the dialog has two controls, a WebView, and an NSTextField: Even if I force the exit of runModalForWindow: by clicking on the window, TABbing between the two controls remains screwed up — it simply never works again. It's like my event loop is horked.
I've tried changing validationComplete: to instead post a notification to the main thread, and I've also played with the waitUntilDone on the performSelectorOnMainThread method, all to no effect.
Any ideas? Things I should try looking at?
From the NSApplication documentation:
abortModal must be used instead of
stopModal or stopModalWithCode: when
you need to stop a modal event loop
from anywhere other than a callout
from that event loop. In other words,
if you want to stop the loop in
response to a user’s actions within
the modal window, use stopModal;
otherwise, use abortModal. For
example, use abortModal when running
in a different thread from the
Application Kit’s main thread or when
responding to an NSTimer that you have
added to the NSModalPanelRunLoopMode
mode of the default NSRunLoop.
So, I learned something today.