-1

I have this method here:

- (IBAction)checkBoxTapped:(id)sender forEvent:(UIEvent*)event
{

    NSLog(@"%@", sender);
}

this is what sender is returning...

enter image description here

My question is how do I get the value of _checked

Checkbox is a class

Please Help

user979331
  • 11,039
  • 73
  • 223
  • 418
  • `Checkbox *checkBox = (Checkbox *)sender;`, then `BOOL isCheckBoxChecked = checkBox.checked`? – Larme Jan 14 '16 at 15:34
  • Possible duplicate of [Objective C: what is a "(id) sender"?](http://stackoverflow.com/questions/5578139/objective-c-what-is-a-id-sender) – dandan78 Jan 14 '16 at 15:40

2 Answers2

1

Without seeing the interface we can only guess what the getter method is for that property. As it's BOOL, and following convention, it's probably called isChecked as it's probably declared as:

 @property (readonly, getter=isChecked) BOOL checked;

Therefore, simply access the isChecked method (the NSAssert() is entirely optional):

NSAssert([sender isKindOfClass:[Checkbox class]], @"Expected a Checkbox instance");
Checkbox *checkbox = (Checkbox *)sender;
if (checkbox.isChecked) {
    // Do thing
}
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • this gives me an error...No visible @interface for 'Checkbox' declares the selector 'checked' – user979331 Jan 14 '16 at 15:39
  • @user979331 Where is `Checkbox` declared (please edit your question)? – trojanfoe Jan 14 '16 at 15:40
  • Try `isChecked` instead of `checked`. – rmaddy Jan 14 '16 at 15:54
  • 1
    @rmaddy Highly likely, however `Checkbox` is a custom class is it not and I am not privvy to its interface. – trojanfoe Jan 14 '16 at 15:56
  • @rmaddy Having said that I've edited my answer to use `isChecked` as that is most likely. – trojanfoe Jan 14 '16 at 19:34
  • But wouldn't it still be just `checked` when using property syntax and `isChecked` when using the getter method call? – rmaddy Jan 14 '16 at 20:09
  • @rmaddy No it would be the same in both cases wouldn't it? Or am I missing something? – trojanfoe Jan 14 '16 at 21:15
  • @trojanfoe OK, I just tried a few examples. If the property's getter is defined to be `isChecked` then if you use the getter method call, it must be `[self isChecked]`. If you use the property syntax, it should be `self.checked` but since you can call methods with no parameters using the property syntax, using `self.isChecked` does work but I personally feel it's wrong. Either use the property as `self.checked` or the getter method as `[self isChecked]`. – rmaddy Jan 14 '16 at 21:31
0

You can replace id with Checkbox class in a method signature like that:

- (IBAction)checkBoxTapped:(Checkbox *)sender forEvent:(UIEvent*)event {
    NSLog(@"%@", sender.checked ? @"Checked" : @"Not checked");
}

Probably, you are not going to connect this method to another control that is not a Checkbox.

Also you need to move checked property to header file (public interface) of Checkbox class

Anton Tyutin
  • 626
  • 1
  • 5
  • 15