View Full Version : NSRunAlertPanel
dadidoe
February 18th, 2009, 12:25 PM
I have the following NSRunAlertPanel:
NSRunInformationalAlertPanel(@"The Serial provided was correct!", @"Thank you for registering WriteIt!.Keep your serial,name you registered with and e-mail you registered at a safe place, it may be needed later. Have fun using WriteIt!", @"OK", nil, nil);
else
NSRunCriticalAlertPanel(@"Serial Incorrect!", @"The Serial you have entered is incorrect.Please try again.", @"Retry", nil, nil);
How can I specify an action for the OK button? I need it to change a BOOL in my apps preference file.
dadidoe
February 19th, 2009, 12:35 PM
ok change of question. How can I make a CheckBox get ticked via objective c?
ElDiabloConCaca
February 19th, 2009, 01:43 PM
Since a checkbox is really just an NSButton, you can use the NSButton class/accessors to get/set the state of the checkbox.
http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSButton_Class/Reference/Reference.html
Look around for "state" and "setState".
dadidoe
February 19th, 2009, 03:29 PM
i'm new to cocoa, could you maybe give me an example? because - (void)setState:(NSInteger)value does not tell me much. I have 2 checkboxes that need to be checked or unchecked (don't care what way).
any help is greatly apprechiated
Mikuro
February 19th, 2009, 03:52 PM
The documentation (http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/ApplicationKit/Classes/NSButton_Class/Reference/Reference.html#//apple_ref/occ/instm/NSButton/setState:) describes what that NSInteger means. It needs to be one of the three constants defined for button states: NSOnState, NSOffState, or NSMixedState.
Quick example:
[myCheckbox setState:NSOnState]; //checks the box
[myCheckbox setState:NSOffState]; //unchecks the box
//Toggle like so:
if ([myCheckbox state]==NSOnState) [myCheckbox setState:NSOffState];
else [myCheckbox setState:NSOnState]
dadidoe
February 19th, 2009, 04:21 PM
I will try this tomorrow...hope it works,it's the onliest thing missing in my apps licence dialog before release:)
I see you use mycheckbox as the name.Do I need to name the checkbox in the .h file or give it a Tag in interface builder?
Mikuro
February 19th, 2009, 04:35 PM
There are two main ways to access controls from your code. One is to make an IBOutlet in your .h file and connect it to the control in Interface Builder. The other is to set the tag of the control in Interface Builder and do something like [[<window> contentView] viewWithTag:1].
The first option is probably the way to go, since it makes for simpler, more readable code.
dadidoe
February 20th, 2009, 08:35 AM
ok this has partly worked. Yes, I defined the outlets here:
IBOutlet NSButton *checkboxserial;
IBOutlet NSButton *checkboxlimit;
then set the state:
- (void)setState:(NSInteger)sender
{
[checkboxlimit setState:NSOffState]; //unchecks the box
}
but I am receiving 2 errors here:
if([[crypto clearTextAsString] isEqualToString:details])
([checkboxlimit state]==NSOnState) [checkboxlimit setState:NSOffState];
([checkboxserial state]==NSOnState) [checkboxserial setState:NSOffState];
//or maybe NSRunInformationalAlertPanel(@"The Serial provided was correct!", @"Thank you for registering WriteIt!.Keep your serial,name you registered with and e-mail you registered at a safe place, it may be needed later. Have fun using WriteIt!", @"OK", nil, nil);
else
NSRunCriticalAlertPanel(@"Serial Incorrect!", @"The Serial you have entered is incorrect.Please try again.", @"Retry", nil, nil);
[crypto release];
}
@end
What it is meant to do is tick the two checkboxes "checkboxlimit" and "checkbox serial" if a correct key has been provided. The error i am receiving is SYNTAX ERROR before setState:
Do you know why I am receiving this error and how can I solve it?
Mikuro
February 20th, 2009, 08:50 AM
Two main problems: You're missing the braces in your if-else statement, and the two setState lines are composed like 'if' statements when they shouldn't be (and are missing the "if", which is causing a compile error).
Try this instead:
if([[crypto clearTextAsString] isEqualToString:details]) {
[checkboxlimit setState:NSOffState];
[checkboxserial setState:NSOffState];
//or maybe NSRunInformationalAlertPanel(@"The Serial provided was correct!", @"Thank you for registering WriteIt!.Keep your serial,name you registered with and e-mail you registered at a safe place, it may be needed later. Have fun using WriteIt!", @"OK", nil, nil);
} else {
NSRunCriticalAlertPanel(@"Serial Incorrect!", @"The Serial you have entered is incorrect.Please try again.", @"Retry", nil, nil);
}
[crypto release];
Looks like you might be coming from Python where all you need to do to define the scope of an IF statement is to indent the following lines. In C/C++/Objective-C, you need to use braces (except for single-line statements).
dadidoe
February 20th, 2009, 10:05 AM
Yes!! The checkboxes now do get ticked indeed. The Bindings that the checkboxes have don't though. The checkboxes are meant to activate a BOOL value, which they do when checked manually but don't when the serial have been entered. Why are the bindings ignored in this case?
dadidoe
February 20th, 2009, 10:41 AM
maybe use NSDictionary to change the 2 BOOLs? how i don't really know..
dadidoe
February 20th, 2009, 11:11 AM
Managed to do it with NSDictionary, but i have one problem.
[plist writeToFile:@"/file.plist" atomically:YES];
It wirites the file, but i need it to go to user/library. How can I do that? I mean not everyone's called David?
Mikuro
February 20th, 2009, 11:14 AM
Off the top of my head, I don't know why that is. I guess setState is not a trigger for key-value coding. :\
But if you're using bindings, you might want to try going the other route: instead of setting the checkbox's state, set the BOOL it's bound to using [<whatever_object_controls_the_bool> setValue:[NSNumber numberWithBool:FALSE] forKey:<your_BOOL's_name>], and let the checkbox update automatically via the binding.
You mentioned a dictionary. Is that where you're currently storing the BOOLs?
Edit: Never mind, I started writing this before I saw your last post.
As for your new question, try using [@"~/Library/Preferences/file.plist" stringByExpandingTildeInPath] instead of @"/file.plist". "~" is Unix shorthand for the current user's home folder.
dadidoe
February 20th, 2009, 11:21 AM
Thank you very much, it is all working now:)
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by
vBSEO 3.3.0 RC1