Creating a Modal Dialog or Window in Cocoa Objective-C? - objective-c

I need to create an modal dialog, which is to loaded from a nib file and should be displayed on a button click in the main window.
I can create a custom window in a nib file and load the custom dialog on button click, but it's not a modal dialog. I can switch back to the main window.
MyWindowController is the NSWindowController subclass.
I used the code below to display my window in response to the button event:
MyWindowController *pController = [[MyWindowController alloc]
initWithWindowNibName:#"nibfilename"];
[MyWindowController showWindow:self];

There are several ways to do this — and in fact two different kinds of modal dialog in OS X: application-modal and window-modal. Using the wrong one will annoy people. One is a sheet, which is attached to the window that it affects (save dialogs are an example of this). The other is application-modal, which blocks the whole application (open dialogs work this way, since they don't apply to any window that exists yet). Apple's sheet documentation should help get you oriented.

Thank you...
(Example).
Create a nib with name "About"
if(pAbtCtrl == nil)
pAbtCtrl = [[AboutWindowController alloc] initWithWindowNibName:#"About"];
pAbtWindow = [pAbtCtrl window];
[NSApp runModalForWindow: pAbtWindow];
[NSApp endSheet: pAbtWindow];
[pAbtWindow orderOut: self];

Related

NSPanel Sheet Not Receiving Mouse Events

SIMPLIFIED:
I have the following:
NSWindow (this is the main window of the app)
NSPanel (this is presented as a sheet, over the main NSWindow, using [NSApp beginSheet:)
The main NSWindow has a subclassed NSButton that uses mouse events, including mouseEntered, mouseExited. This changes the background color of the button as I hover over it.
I present my NSPanel, this also has an NSButton of the same subclass, to change color on hover.
Mouse events still occur in the main NSWindow while my NSPanel is presented. I can tell this, as hovering over the buttons still works.
However, hovering over the buttons in the presented NSPanel does not work, despite them being no different from the other buttons. I can click on them and they receive an action. But they do not call the mouse events.
ORIGINAL:
I have a simple setup in my Mac app. I have a main NSWindow, as any usual mac app, then I run the following code which displays my detail view.
My detail view is an NSWindowController subclass and I am displaying it as a sheet. This is setup as an NSPanel in the xib and just displays a detail view for me over my main window.
APDetailWindowController *vc = [[APDetailWindowController alloc] initWithWindowNibName:#"APDetailWindowController"];
self.detailWindowController = vc;
self.detailWindowController.delegate = self;
APAppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
[NSApp beginSheet:self.detailWindowController.window modalForWindow:appDelegate.window modalDelegate:self didEndSelector:nil contextInfo:nil];
In my main window (of the AppDelegate), I have my views played out as any app, including some NSButtons. These buttons are subclassed so that when they receive mouseEntered and mouseExited events, they change colour. Basically creating a rollover effect.
In my detail view, I also have buttons of the same subclass. However, they are not receiving the mouse events at all. I can see when moving my mouse over them nothing happens. Even when the detail view is displayed, the rollover works on the main window buttons in the background, but not in my detail view.
I am thinking maybe some sort of focus is still on the main window and needs to be set to the detail window?

How to open an NSWindowController from another NSWindowController in Cocoa

I'm developing an app in cocoa for MacOSX in Xcode5 and I want to open another window from my current window by pressing a button, this is my code:
- (IBAction)openWindow:(id)sender {
WindowController *controllerWindow = [[WindowController alloc] initWithWindowNibName:#"WindowController"];
[controllerWindow showWindow:nil];
[[controllerWindow window] makeMainWindow];
}
so far I can see the window appearing by one second and then this just dissappear, how to do this correctly???
Neither the window nor the window controller have a strong reference anywhere outside the scope of this method.
So after that, they're getting released.
Normally, you would add your window controller to some container like an array in your app delegate.
The array will retain the window controller.
The window controller can hang on to the window.
It also makes sense for the action method to be in the app delegate. You button should just send a selector up the responder chain.
use this..
Create a new .h & .m files which yo need to open, as NewWindowController (for eg.) along with its .xib
And on any button click, to open the newly defined window, just allocate, instantiate and present..
NewWindowController *controllerWindow = [[NewWindowController alloc] initWithWindowNibName:#"NewWindowController"];
[controllerWindow showWindow:self];

App stuck on runModalForWindow

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.

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 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");
}

How to open a window on click on NSStatusItem?

I'm pretty new to cocoa, so please excuse me for any stupid mistakes I make.
I have a NSStatusItem, which I want to use to open up a menu. However as far as I know and have heard across different forms, without a custom view you are restricted to just a pop down menu. Is this true? And if so how do you make a custom view to do something (e.g. open a window in my case)? Thanks for any help.
No, it is not true. You need to set up the target and action for the status item to call a method which does what you want (opens the window).
// This goes where you set up the status item
NSStatusItem *statusItem; // You need to get this from the status bar
[statusItem setTarget:self];
[statusItem setAction:#selector(openWindow:)];
// This method is called when the status item is clicked
- (void)openWindow:(id)sender {
NSWindow *window = [self window]; // Get the window to open
[window makeKeyAndOrderFront:nil];
}
You may also want to call [NSApp activateIgnoringOtherApps:nil]; to your openWindow: method to ensure that the window you open is not behind some other application's window.