checkboxes and controls.

Discussion in 'Mac Programming' started by kristiaand, Jan 11, 2008.

  1. kristiaand macrumors member

    Joined:
    Dec 5, 2007
    #1
    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?
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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?
     
  3. kristiaand thread starter macrumors member

    Joined:
    Dec 5, 2007
    #3

    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
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Have you looked at the numerous examples in /Developer/Examples/AppKit/ ?
     
  5. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #5
    You really should go get Aaron Hillegass' book Cocoa Programming for Mac OS X and go through it start to finish. The whole book is example code and very easy to follow. I guarantee you'll know a lot more about Cocoa after reading it.
     
  6. DVD Plaza macrumors member

    Joined:
    Jan 12, 2008
    Location:
    adelaide.sa.au
    #6
    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.
     
  7. kristiaand thread starter macrumors member

    Joined:
    Dec 5, 2007
    #7
    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)
     
  8. DVD Plaza macrumors member

    Joined:
    Jan 12, 2008
    Location:
    adelaide.sa.au
    #8
    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.
     
  9. Macgenie macrumors newbie

    Joined:
    Jan 8, 2008
    Location:
    Suffolk, UK
    #9
    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.
     
  10. apffal macrumors newbie

    Joined:
    Feb 10, 2008
    #10
    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 ?
     
  11. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #11
    Those are just variables in your class. They can be whatever you want.
     
  12. apffal macrumors newbie

    Joined:
    Feb 10, 2008
    #12
    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 ?
     
  13. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #13
    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;
     
  14. apffal macrumors newbie

    Joined:
    Feb 10, 2008
    #14
    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 ?
     
  15. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #15
    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.
     
  16. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #16
    Here's a simple project that demonstrates binding a checkbox using properties if you're still having trouble with it.
     

    Attached Files:

  17. apffal macrumors newbie

    Joined:
    Feb 10, 2008
    #17
    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];
    }
     
  18. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #18
    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.
     
  19. apffal macrumors newbie

    Joined:
    Feb 10, 2008
    #19
    bindings

    These are my nib and bindings palette for checkbox

    /users/apffal/Picture 1

    /users/apffal/Picture 2
     
  20. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #20
    You need to upload your images via the attachments icon:
    attachments.jpg
     
  21. apffal macrumors newbie

    Joined:
    Feb 10, 2008
  22. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #22
    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?
     
  23. apffal macrumors newbie

    Joined:
    Feb 10, 2008
    #23
    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 ?
     

Share This Page