issac
issac

Reputation: 1501

How to pass a variable to a UIButton action

I want to pass a variable to a UIButton action, for example

NSString *string=@"one";
[downbutton addTarget:self action:@selector(action1:string)
     forControlEvents:UIControlEventTouchUpInside];

and my action function is like:

-(void) action1:(NSString *)string{
}

However, it returns a syntax error. How to pass a variable to a UIButton action?

Upvotes: 16

Views: 31042

Answers (8)

Miguel Gallego
Miguel Gallego

Reputation: 447

You can use the strings of the UIControlStates that you dot'n use:

NSString *string=@"one";
[downbutton setTitle:string forState:UIControlStateApplication];
[downbutton addTarget:self action:@selector(action1:) forControlEvents:UIControlEventTouchUpInside];

and the action function:

-(void)action1:(UIButton*)sender{
    NSLog(@"My string: %@",[sender titleForState:UIControlStateApplication]);
}

Upvotes: 0

Andrea Fini
Andrea Fini

Reputation: 64

You can extends UIButton and add a custom property

//UIButtonDictionary.h
#import <UIKit/UIKit.h>

@interface UIButtonDictionary : UIButton

@property(nonatomic, strong) NSMutableDictionary* attributes;

@end

//UIButtonDictionary.m
#import "UIButtonDictionary.h"

@implementation UIButtonDictionary
@synthesize attributes;

@end

Upvotes: 1

Fawad Masud
Fawad Masud

Reputation: 12344

You can set tag of the button and access it from sender in action

[btnHome addTarget:self action:@selector(btnMenuClicked:)     forControlEvents:UIControlEventTouchUpInside];
                    btnHome.userInteractionEnabled = YES;
                    btnHome.tag = 123;

In the called function

-(void)btnMenuClicked:(id)sender
{
[sender tag];

    if ([sender tag] == 123) {
        // Do Anything
    }
}

Upvotes: 0

Eddy
Eddy

Reputation: 3703

Another option for passing variables, which I find to be more direct than the tag from leviatan's answer is to pass a string in the accessibilityHint. For example:

button.accessibilityHint = [user objectId];

Then in the action method of the button:

-(void) someAction:(id) sender {
    UIButton *temp = (UIButton*) sender;
    NSString *variable = temp.accessibilityHint;
    // anything you want to do with this variable
}

Upvotes: 6

Heitor Ferreira
Heitor Ferreira

Reputation: 61

You can use associative references to add arbitrary data to your UIButton:

static char myDataKey;
...
UIButton *myButton = ...
NSString *myData = @"This could be any object type";
objc_setAssociatedObject (myButton, &myDataKey, myData, 
  OBJC_ASSOCIATION_RETAIN);

For the policy field (OBJC_ASSOCIATION_RETAIN) specify the appropriate policy for your case. On the action delegate method:

(void) buttonPress:(id)sender {
  NSString *myData =
    (NSString *)objc_getAssociatedObject(sender, &myDataKey);
  ...
}

Upvotes: 6

leviathan
leviathan

Reputation: 11161

If you need to distinguish between multiple buttons, then you could mark your buttons with tags like this:

[downbutton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
downButton.tag = 15;

In your action delegate method you can then handle each button according to its previously set tag:

(void) buttonPress:(id)sender {
    NSInteger tid = ((UIControl *) sender).tag;
    if (tid == 15) {
        // deal with downButton event here ..
    }
    //...
}

UPDATE: sender.tag should be a NSInteger instead of a NSInteger *

Upvotes: 18

ennuikiller
ennuikiller

Reputation: 46965

The only way I've found to do this is set an instance variable before calling the action

Upvotes: 1

diciu
diciu

Reputation: 29333

Change it to read:

[downbutton addTarget:self action:@selector(action1:) forControlEvents:UIControlEventTouchUpInside];

I don't know about the Iphone SDK, but the target of a button action probably receives an id (usually named sender).

- (void) buttonPress:(id)sender;

Within the method call, sender should be the button in your case, allowing you to read properties such as it's name, tag, etc.

Upvotes: 21

Related Questions