# NSRunAlertPanel



## dadidoe (Feb 18, 2009)

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 (Feb 19, 2009)

ok change of question. How can I make a CheckBox get ticked via objective c?


----------



## ElDiabloConCaca (Feb 19, 2009)

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/document...asses/NSButton_Class/Reference/Reference.html

Look around for "state" and "setState".


----------



## dadidoe (Feb 19, 2009)

i'm new to cocoa, could you maybe give me an example? because - (void)setStateNSInteger)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 (Feb 19, 2009)

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:


```
[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 (Feb 19, 2009)

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 (Feb 19, 2009)

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 (Feb 20, 2009)

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 (Feb 20, 2009)

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 (Feb 20, 2009)

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 (Feb 20, 2009)

maybe use NSDictionary to change the 2 BOOLs? how i don't really know..


----------



## dadidoe (Feb 20, 2009)

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 (Feb 20, 2009)

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 (Feb 20, 2009)

Thank you very much, it is all working now


----------

