1

is there a way to pass a boolean to the addClicked method beside using button.tag for the code below?

[cellview.buttonAdd addTarget:self action:@selector(addClicked:) forControlEvents:UIControlEventTouchUpInside];

-(void) addClicked:(id)sender {

}

THanks in advance.

tipsywacky
  • 3,374
  • 5
  • 43
  • 75
  • I don't know how your button would determine the boolean flag (you might have to explain what you're trying to do), but the common way to determine some value associated with the button would be the `tag` property, used to distinguish between various controls or `UIView` objects. If you need some other boolean, you could presumably subclass `UIButton`, add your own boolean property, and read that in addClicked (but then again, you could accomplish that via some ivar, too). – Rob Aug 02 '12 at 03:46
  • I already assigned index.path to the tag, so I am looking for another way to pass the boolean. I want to use a boolean to check if an alert view is shown. The boolean is declared at a tableview custom view cell and I want to be able to use it at the addClicked method. Any help would be grateful! – tipsywacky Aug 02 '12 at 03:53
  • 1
    Like I said, if you can't use the `tag`, you could subclass `UIButton` with your own `BOOL` property, as michael_mackenzie has demonstrated. – Rob Aug 02 '12 at 04:02

3 Answers3

3

if you want to add a integer property , you can use tag. if you want to add a nonInteger property , you must use a category with Associative References,the inheritting UIButton can not post property at all. you can see this :

Subclass UIButton to add a property

Community
  • 1
  • 1
cloosen
  • 993
  • 7
  • 17
2

Try something like this:

-(void) addClicked:(id)sender
{
    UIButton * button = (UIButton*)sender;
    NSLog(@"Button Tag: %i", button.tag);
}

Not sure what you mean by pass Boolean.

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • i already assigned the indexPath.row to the tag, so i'm looking for a way to use cellview.showBooleanItem. ty in advance. – tipsywacky Aug 02 '12 at 03:55
  • You need to post code you are using that is relevant, like what you have mentioned, in your question. – WrightsCS Aug 02 '12 at 03:57
1

Short answer: You cannot pass extra information into the method directly.

Why would you want to do that anyway though? What does the button "know" that it would need to communicate, other than the fact that it was clicked?

The way this should be done is via an instance variable in the class that implements the click handler.

If you really must maintain state inside the button itself, subclass it:

@interface CustomButton : UIButton

@property (nonatomic, assign) BOOL myBoolValue;

@end

/* ... */

- (void)addClicked:(id)sender
{
    CustomButton *button = (CustomButton *)sender;
    if (button.myBoolValue) {
        // Whatever you want to do.
    }
}
mamackenzie
  • 1,156
  • 7
  • 13