NSRunAlertPanel

Status
Not open for further replies.

dadidoe

Computer Pro :)
I have the following NSRunAlertPanel:

Code:
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.
 
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
 
The documentation describes what that NSInteger means. It needs to be one of the three constants defined for button states: NSOnState, NSOffState, or NSMixedState.

Quick example:

Code:
[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]
 
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?
 
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.
 
ok this has partly worked. Yes, I defined the outlets here:

Code:
	IBOutlet NSButton *checkboxserial;
	IBOutlet NSButton *checkboxlimit;

then set the state:

Code:
- (void)setState:(NSInteger)sender
{
	[checkboxlimit setState:NSOffState]; //unchecks the box
}

but I am receiving 2 errors here:

Code:
	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?
 
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:
Code:
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).
 
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?
 
Managed to do it with NSDictionary, but i have one problem.

[plist writeToFile:mad:"/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?
 
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.
 
Status
Not open for further replies.
Back
Top