Re: NSRunAlertPanel, Correct Usage
Re: NSRunAlertPanel, Correct Usage
- Subject: Re: NSRunAlertPanel, Correct Usage
- From: Sean Murphy <email@hidden>
- Date: Wed, 6 Sep 2006 13:10:16 -0400
On Sep 6, 2006, at 12:03 PM, digital.pardoe wrote:
I wish to have my application receive a users response on the very
first run of an application. I found the code that provided me with
a way to do this in the Sparkle updater framework and have
implemented it so that it works successfully;
NSNumber *defaultsVar = [[NSUserDefaults standardUserDefaults]
objectForKey:@"MyDefault"];
if (!defaultsVar)
{
defaultsVar = [NSNumber numberWithBool:NSRunAlertPanel
(SULocalizedString(@"Enable control?"), [NSString
stringWithFormat:SULocalizedString(@"Would you like control to be
enabled?")], SULocalizedString(@"Yes"), SULocalizedString(@"No"),
nil) == NSAlertDefaultReturn];
[[NSUserDefaults standardUserDefaults] setObject:defaultsVar
forKey:@"MyDefault"];
}
Essentially I want to remove the references to the 'Sparkle' method;
SULocalizedString
I have read the documentation for NSRunAlertPanel, looked at other
examples and tried a few different ways (probably wrong) of
implementing the strings but they never compile. Even with the
above I get the error 'makes pointer from integer without a cast'.
Could someone advise me as how to correct this, or how to correctly
implement it to perform what I would like it to do?
In the Sparkle framework code, Andy is using SULocalizedString() to
return an NSString which, unsurprisingly, is a localized value for
the supplied argument. So, you can remove references to
SULocalizedString and just replace them with a standard NSString
object. Looking at the documentation for NSRunAlertPanel shows
exactly what type of parameters/return values this function has:
int NSRunAlertPanel(NSString *title, NSString *msg, NSString
*defaultButton, NSString *alternateButton, NSString *otherButton, ...)
You can provide an NSString constant anywhere an NSString object is
required, meaning your code can be as simple as this:
NSNumber *defaultsVar = [NSNumber numberWithBool:NSRunAlertPanel
(@"Enable control?", @"Would you like control to be enabled?",
@"Yes", @"No", nil) == NSAlertDefaultReturn];
NSRunAlertPanel is used as an inline function here, as part of an
expression which compares the return value of the alert panel to
NSAlertDefaultReturn, which is just an enum'd integer (NSRunAlert
panel's return type, from the above documentation). The entire
expression is then stored as a an NSNumber and inserted into the
defaults. The next time this method is called, it checks the user
defaults for this key, and if implemented, uses the value it was set to.
I'd also encourage you to consider a few more points which will make
your Cocoa app better in the long run.. First, it is helpful (and
more Cocoa-like) to give variables very clear names, instead of
generic descriptions such as defaultsVar (maybe shouldEnableControl
would be a better choice here.) Second, the infamous "Yes, No,
Cancel" type dialogs are discouraged and out of place on Mac OS X.
They force a user to read the entire alert messsage. The dialog you
present would offer a better user experience, and make it clear what
action each button is actually performing, if the they were labeled
with verbs such as "Enable Control" and "Don't Enable Control." This
attentiveness to user-experience (and code style) demonstrate 'paying
attention to detail', which is a trait that exemplifies Mac software
as a whole.
References:
Apple Human Interface Guidelines:
<http://developer.apple.com/documentation/UserExperience/Conceptual/
OSXHIGuidelines/index.html>
Cocoa Coding Style
<http://developer.apple.com/documentation/Cocoa/Conceptual/
CodingGuidelines/index.html>
Scott Stevenson's Cocoa Style for Objective-C
<http://cocoadevcentral.com/articles/000082.php>
<http://cocoadevcentral.com/articles/000083.php>
Hope that helps..
-Sean
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden