Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kristiaand

macrumors member
Original poster
Dec 5, 2007
63
0
hi all,
im starting to feel like i am alone in this big world of cocoa and objective C.

i have just spent 10 minutes googling on howto read the state of a checkbox in cocoa and cannot find ANYTHING of use not even on this forum...

can someone point me in the direction of some really useful beginner tutorials i have been to cocoadev and cocoadevcentral and there is some good stuff on there but nothing that answers the obvious little questions.

dont suppose someone could post example code please?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Checkboxes are just normal NSButtons, which can have a state of NSOnState, NSOffState, or NSMixedState. See the state and setState: methods.

Have you read the Cocoa documentation?
 

kristiaand

macrumors member
Original poster
Dec 5, 2007
63
0
Checkboxes are just normal NSButtons, which can have a state of NSOnState, NSOffState, or NSMixedState. See the state and setState: methods.

Have you read the Cocoa documentation?


hi kainjow thanks for the reply i have tried there documentation and to be honest its very well documented and clear, but i tend to learn from example code, where i can see how its used and play around with it. Delphi was very good for this you always got a nice simple example with each controls eplanation showing how its used so i picked it up quicker.

nothing i have found on the internet yet does this or seems to be geared for beginners.

this is an example of what i have found its very well documented but there is no indication of HOW you use it, apples site is not really geared up for someone learning objective C

http://developer.apple.com/documentation/Cocoa/Conceptual/Button/Tasks/SettingButtonType.html
 

DVD Plaza

macrumors member
Jan 12, 2008
70
89
adelaide.sa.au
i have just spent 10 minutes googling on howto read the state of a checkbox in cocoa and cannot find ANYTHING of use not even on this forum... dont suppose someone could post example code please?
1. Throw a checkbox and a button onto your window.
2. In your delegate's header throw an iboutlet for the checkbox and an ibaction for the button. Eg:
Code:
 IBOutlet NSButton *CheckBox;
 - (IBAction) CheckBoxStatus:(id) sender;
3. Throw the following code into your delegate's code:
Code:
- (void) CheckBoxStatus:(id) sender
{
	NSLog([CheckBox state] ? @"Checkbox is ON" : @"Checkbox is OFF");
}
4. Build and Go - clicking the button will then tell you in the debugger console whether the checkbox is currently ticked or not. The state method simply returns you a boolean whether the checkbox is ticked or not.
 

kristiaand

macrumors member
Original poster
Dec 5, 2007
63
0
1. Throw a checkbox and a button onto your window.
2. In your delegate's header throw an iboutlet for the checkbox and an ibaction for the button. Eg:
Code:
 IBOutlet NSButton *CheckBox;
 - (IBAction) CheckBoxStatus:(id) sender;
3. Throw the following code into your delegate's code:
Code:
- (void) CheckBoxStatus:(id) sender
{
	NSLog([CheckBox state] ? @"Checkbox is ON" : @"Checkbox is OFF");
}
4. Build and Go - clicking the button will then tell you in the debugger console whether the checkbox is currently ticked or not. The state method simply returns you a boolean whether the checkbox is ticked or not.

Hi Thanks for the info, i have one little question which always seems to make me wonder, i have a simple program i made which does the standard hello world with a button and a static text object, i added the code to the existing project but when i try to attach the new button and check box to the existing class instance it only shows up the original action and output that i created when i instantiated the class??

how to i get the new ones to show up?

also i am waiting for payday to get Aaron hillgrasses book as its been suggested to me by a few other people, (i did ask for it for xmas but no joy)
 

DVD Plaza

macrumors member
Jan 12, 2008
70
89
adelaide.sa.au
but when i try to attach the new button and check box to the existing class instance it only shows up the original action and output that i created when i instantiated the class??

how to i get the new ones to show up?
So your header file looks something like this?
Code:
@interface YourDelegateName : NSObject {
 IBOutlet NSTextField *YourLabelName;
 IBOutlet NSButton *YourCheckBoxName;
}

- (IBAction) YourFirstButtonName:(id) sender;
- (IBAction) YourSecondButtonName:(id) sender;

@end
If Interface Builder isn't showing these up when you right-click and drag you probably forgot to hit save in Xcode - switch back to the header file in Xcode, hit Command-S or File/Save, switch back to Interface Builder, and try again.
 

Macgenie

macrumors newbie
Jan 8, 2008
14
0
Suffolk, UK
Don't forget bindings

An alternative way to react to switch settings is to use the bindings scheme - once you have getter & setter methods in your model class i.e.

// getter method in model class:

- (BOOL)switch {
return switch;
}

// setter method in model class:

-(void)setSwitch:(BOOL)flag {
switch = flag;
}

.... you can then bind the actual switch in IB to use these switch methods - the binding will then keep your switch and the instance variable (switch) synchronised. Within your model class all you then have to do is to use the switch variable like this:

- (void)reactToSwitch {
if (switch) {
// do this .... }
else {
do this instead ....}
}

To set up the binding do this:

Open Interface Builder (IB)
Click on your switch in the window
The inspector should now be titled "NSButton inspector"
Select the Bindings pane
Select the Value
There are 3 settings here:

1 - Bind to - type your model class name here
2 - Controller key - select the "Selection" key
3 - Model key path - type in the name of your getter method i.e. from the above example type: switch

Once the app is running you will find that Cocoa keeps the switch variable to the same boolean state as your interface switch.
 

apffal

macrumors newbie
Feb 10, 2008
24
0
Bindings

I tried the bindings example, but got always a syntax error.
Is it not necessary to set properties for "switch" and "flag" variables ?
And how to do it ?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
I tried the bindings example, but got always a syntax error.
Is it not necessary to set properties for "switch" and "flag" variables ?
And how to do it ?

Those are just variables in your class. They can be whatever you want.
 

apffal

macrumors newbie
Feb 10, 2008
24
0
Bindings

In my header file I've added these properties

@property(assign,getter=switch) BOOL switch;
@property(copy,setter=setSwitch:) BOOL flag;

and method definitions

-(BOOL)switch;
-(void)setSwitch(BOOL)flag;

And I get 2 errors
"syntax error before 'switch'" (reported to first property line)
"method definition not in @implementation context"

What's wrong ?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
The point of properties in Obj-C 2 is so you don't have to define your own accessor methods. You can if you want but most of the time it's unnecessary. So just define your property like normal without custom getter and setter methods, and then bind your checkbox to the property itself.

Regarding your syntax error:

Code:
-(void)setSwitch(BOOL)flag;

should be...

Code:
-(void)setSwitch[color=red]:[/color](BOOL)flag;
 

apffal

macrumors newbie
Feb 10, 2008
24
0
Bindings

I' ve done it and now I receive this warning:
"local declaration of 'flag' hides instance variable"

And my checkbox state isn't still recognized.
Where must the void method be declared ?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
You're getting that error about "flag" because you're using flag as a local variable (parameter in the method) and as a class member variable. It's not good to do that. Change the name of "flag" in your method to "aFlag" or something else.

Or just don't use custom accessor methods because it's unneeded.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Here's a simple project that demonstrates binding a checkbox using properties if you're still having trouble with it.
 

Attachments

  • CheckboxBindings.zip
    38.4 KB · Views: 551

apffal

macrumors newbie
Feb 10, 2008
24
0
bindings

Thank you for your code example - but I think it doesn' t full applies to my case.
Now, I compile without errors, but still get result which ignores the checkbox state.

I would be very gratefull if you examine my complete code ... and I promise not asking more questions !

In IB the checkbox (optBox) is bound to opt - so, binding value is Calc.opt

Calc.h

#import <Cocoa/Cocoa.h>

@interface Calc : NSObject {

BOOL opt;
float venc, taxa, inc, p;
int idade, ref, vu;
IBOutlet NSButton *optBox;
IBOutlet NSTextField *vencField;
IBOutlet NSTextField *idField;
IBOutlet NSTextField *refField;
IBOutlet NSTextField *taxField;
IBOutlet NSTextField *incField;
IBOutlet NSTextField *valField;
Calc *calc;
}

@property(readwrite) BOOL opt;
@property(readwrite) float venc, taxa, inc, p;
@property(readwrite) int idade, ref, vu;

-(BOOL)opt;

-(float)calcIndem;

-(IBAction)calc:(id)sender;

@end


Calc.m

#import "Calc.h"

@implementation Calc

@synthesize opt;
@synthesize venc, taxa, inc, p;
@synthesize idade, ref, vu;

-(BOOL)opt {
return opt;
}

-(float)calcIndem {

if(opt == 1)
p = self.venc * 12 * 2/3;
else
p = self.venc * 12 * self.inc / 100;
vu = self.ref - self.idade;
return p /((self.taxa/100)*(pow((1+(self.taxa/100)),vu))/(pow((1+(self.taxa/100)),vu)-1));
}

- (IBAction)calc:(id)sender {

float val;
calc = [[Calc alloc]init];
[calc setVenc:[vencField floatValue]];
[calc setIdade:[idField intValue]];
[calc setRef:[refField intValue]];
[calc setTaxa:[taxField floatValue]];
[calc setInc:[incField floatValue]];
val = [calc calcIndem_a];
[valField setFloatValue:val];
}
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Your opt method is unneeded, but I don't think that would be messing it up. I'd remove it though.

Maybe post a screenshot of your NIB and of the bindings palette for your checkbox. Your code looks fine so I think the problem is elsewhere.

Also when you post code put it in between [code] ... [/code] tags so it's more readable.
 

apffal

macrumors newbie
Feb 10, 2008
24
0
bindings

These are my nib and bindings palette for checkbox

/users/apffal/Picture 1

/users/apffal/Picture 2
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
You need to upload your images via the attachments icon:
attachments.jpg
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Well I don't know what to tell you. It looks like it should work. If you study my example project you might be able to figure it out. Maybe create yourself a blank new project and see if you can get it to work from scratch.

Edit: how do you know it's not working? What is not working as expected?
 

apffal

macrumors newbie
Feb 10, 2008
24
0
bindings

If checkbox is checked (opt == 1) the "p" value would be self.venc * 12 * 2/3
And even when checked "p" always gives me the "else" value self.venc * 12 * self.inc / 100
Anything more to try ?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.