0

my code :

    UIButton *boutonSuppr = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    boutonSuppr.frame = rectBoutonSuppr;
    [boutonSuppr setBackgroundImage: [UIImage imageNamed:@"croixTest.png"] forState:UIControlStateNormal];
    [monScrollView addSubview: boutonSuppr];
    int numb = 10;
    [boutonSuppr addTarget:self action:@selector(boutonSupprAppuye) forControlEvents:UIControlEventTouchUpInside];

My method declared : - (void)boutonSupprAppuye:(int) numero;

My problem is that I need to send a parameter in the method because I have several UIButton. For example, I want to send "(int)numb" but when I do that :

[boutonSuppr addTarget:self action:@selector(boutonSupprAppuye:numero) forControlEvents:UIControlEventTouchUpInside];

It doesn't work.

Thank you in advance.

Stalyon
  • 538
  • 5
  • 20
  • possible duplicate of [how can i pass an int value through a selector method?](http://stackoverflow.com/questions/7899223/how-can-i-pass-an-int-value-through-a-selector-method) – NobodyNada Jan 19 '15 at 19:21
  • Post the code inside your selector? What doesn't work? – Scott Jan 19 '15 at 19:22
  • possible duplicate of [How to pass variable to new method I call when using @selector(methodname)](http://stackoverflow.com/questions/15163100/how-to-pass-variable-to-new-method-i-call-when-using-selectormethodname) – jlehr Jan 19 '15 at 19:28
  • @NobodyNada The answer you linked addresses doesn't address the OP's question, which is about action methods, not `performSelector:`. – jlehr Jan 19 '15 at 19:31
  • Good question. I actually am not sure if you can pass a value through a selector, I don't believe so... – Henry F Jan 19 '15 at 19:57
  • This is practically a dupe of this question: http://stackoverflow.com/questions/11658694/buttons-for-each-row-uitableview – nielsbot Jan 20 '15 at 01:19
  • At the very least you have to spell the method correctly. The method name includes the `:`. – Hot Licks Jan 20 '15 at 02:51
  • Read the spec: *The action message may optionally include the sender and the event as parameters, in that order.* – Hot Licks Jan 20 '15 at 02:55

3 Answers3

1

If you're really just using an int to identify the button that's being hit you should just set unique tags for your buttons in Interface Builder or when you create them in code, set up corresponding enums and then in your hit method, handle like so. Tags are already available in every UIView and specifically designed for that purpose.

typedef NS_ENUM(NSInteger, ButtonTag) {
    e_buttonTag_unknown = 0,
    e_buttonTag_doThing1Button,    /* 1 */
    e_buttonTag_doThing2Button     /* 2 */
};

...
    // Add this to the button creation code you posted
    boutonSuppr.tag = e_buttonTag_doThing1Button;

    // and set the button's action to @selector(buttonTapped:).
...

- (IBAction) buttonTapped: (id) sender
{
    UIButton    *whichButton = (UIButton *) sender;
    ButtonTag   whichTag = whichButton.tag;

    switch (whichTag)
    {
        case e_buttonTag_doThing1Button:
            // "Do thing 1" button was pressed.
            break;
        case e_buttonTag_doThing2Button:
            // "Do thing 2" button was pressed.
            break;
        default:
            // Some other button was pressed.
            break;
    }
}
clarus
  • 2,455
  • 18
  • 19
0

When you declare a @selector(), it can be considered the same as a function pointer. It does not take parameters at that time; it takes parameters when the selector is called.

This particular selector will be called by the UIButton class when the UIControlEventTouchUpInside event is received. This means that the execution will be out of reach of your source code and any parameters passed into it will be done so automatically by the system.


If you want to attach data to a UIButton, the simplest way to do this is with a @property.

@interface MyButton : UIButton
@property (assign) int numero;
@end
@implementation MyButton
@end

Then your construction becomes:

MyButton *boutonSuppr = [MyButton buttonWithType:UIButtonTypeRoundedRect];
boutonSuppr.numero = numero;
// etc.
Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51
  • I'd say a selector is the name of a method to be called, not quite the same thing as a function pointer. I think that's also an easy to understand concept. – nielsbot Jan 20 '15 at 01:15
  • also, you can add a property to the UIButton class itself using an Obj-C category or Swift extension, avoiding the need to create a new subclass of *UIButton*. – nielsbot Jan 20 '15 at 01:16
  • Thanks for your help. It's a different approach who is very interesting. – Stalyon Jan 20 '15 at 11:42
  • @nielsbot: You can attach to the `UIButton` class, but I tend to try to avoid using C functions in Objective-C code (especially when talking to beginners). – Ian MacDonald Jan 20 '15 at 14:26
  • True--implementing it as a category is more complex. Structurally I think it's prefereable. – nielsbot Jan 20 '15 at 18:29
  • I'm not so sure. When it's implemented as a category, you are exposing that property to anywhere that you use a `UIButton`, even when it isn't applicable. – Ian MacDonald Jan 20 '15 at 18:31
0

Action methods should take an object pointer as argument. Normally this is type id but sometimes a more specific object. The key is it is a pointer and that means you can stuff anything in there in C if you pass in a pointer. For ease just use an object and wrap your value in the object. You could use NSValue for any C type wrapper or NSNumber for scalars

uchuugaka
  • 12,679
  • 6
  • 37
  • 55