PDA

View Full Version : Can't Display NSAlert - Use of Undeclared Identifier




gwelmarten
May 3, 2012, 03:00 PM
Hi
Sorry - this is relatively basic, but I can't get it. I'm trying to display an NSAlert in my document based Mac Application. Unfortunately, I keep getting the error:

Use of undeclared identifier 'window'; did you mean '_windows'?
My code for displaying the NSAlert is:

- (void)showNewUserMessage {
NSAlert *alert = [[NSAlert alloc] init];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert setMessageText:@"IMPORTANT INFORMATION"];
[alert setInformativeText:@"TEXT"];

[alert beginSheetModalForWindow:window modalDelegate:self];
}

and in the related header file, I have:
#import <Cocoa/Cocoa.h>
#import <WebKit/WebView.h>
- (void)showNewUserMessage;


Does anybody know what is causing this? As I understand it, beginSheetModalForWindow:window, 'window' should be the name of the view that the alert should appear in. How would I find that? I have read that I should import foundation/foundation.h in my header, but this didn't make a difference.

Thanks in advanced,
Sam



mfram
May 3, 2012, 04:35 PM
What class is this code in? If it's a subclass of NSView, then you can probably do something like:

[alert beginSheetModalForWindow:[self window] modalDelegate:self];

gwelmarten
May 4, 2012, 12:05 PM
Thanks for that - now I know how to tell it which sheet to display in. However, I am still getting a different error, which I now presume is due to a framework problem.
I've now got:

- (void)showNewUserMessage {
NSAlert *alert = [[NSAlert alloc] init];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert setMessageText:@"IMPORTANT INFORMATION"];
[alert setInformativeText:@"TEXT"];
[alert beginSheetModalForWindow:[self windowForSheet]
modalDelegate:self];

}

Which I call in:

- (void)windowControllerDidLoadNib:(NSWindowController *) aController {
[self showNewUserMessage];
}

My header file has these imports in addition to what I posted previously:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import <AppKit/AppKit.h>

I believe foundation is needed for NSAlert.
The Problem
So basically, when I run it the application appears in the dock but the window does not open. The following is printed in the console:

2012-05-04 17:54:20.609 Anonymous Browser Pro[937:707] -[NSAlert beginSheetModalForWindow:modalDelegate:]: unrecognized selector sent to instance 0x103113290
2012-05-04 17:54:20.610 Anonymous Browser Pro[937:707] -[NSAlert beginSheetModalForWindow:modalDelegate:]: unrecognized selector sent to instance 0x103113290
2012-05-04 17:54:20.612 Anonymous Browser Pro[937:707] -[NSAlert beginSheetModalForWindow:modalDelegate:]: unrecognized selector sent to instance 0x10030ef50
2012-05-04 17:54:20.613 Anonymous Browser Pro[937:707] -[NSAlert beginSheetModalForWindow:modalDelegate:]: unrecognized selector sent to instance 0x10030ef50

And I get this yellow error in Xcode:

Instance method '-beginSheetModalForWindow:modalDelegate:' not found (return type defaults to 'id')

(see attached screenshot)

Any ideas? I've tried fiddeling with the imports and googling the instance method, but I'm not sure.

Thanks for any help in advance,
Sam

chown33
May 4, 2012, 12:39 PM
This codeis trying to call a method that doesn't exist:
[alert beginSheetModalForWindow:[self windowForSheet]
modalDelegate:self];

There is no such method in NSAlert. You may well believe there is such a method, so please point to a reference that shows this method in use, as you've used it.

I suggest reading the class reference doc (https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSAlert_Class/Reference/Reference.html). There is a method whose name begins like your code, but it has two additional parameters and a longer name.

This is not a framework problem, nor can it be fixed by tweaking imports. You must change your code, so it calls the method that exists, using the exact name shown in the reference doc, and supplying all necessary parameters.