views:

1723

answers:

6

How do you add a (id) sender to the following code?

- (IBAction) gobutton: (UIButton *) button5 {

Everything I try fails, any help would be appreciated. Thanks.

EDIT: I need to keep the (UIButton *) button 5 reference in the (IBAction)

+2  A: 

It's not clear what you are trying to do but most actions look like:

- (IBAction) gobutton: (id)sender;
stefanB
I need both the (UIButton *) button5 and (id) sender on the -(IBAction)
0SX
action is usually called by passing the id of sender so you can query the sender, if button5 is connected to gobutton action then the id sender is your button5 ...
stefanB
sender IS the button.
Kendall Helmstetter Gelner
yes if the action is connected to the button then the sender is the button
stefanB
+3  A: 

If I recall correctly, and if you are using this in the way I think you are,

- (IBAction) gobutton: (id) sender {
if(sender == button5)
   //do something...
else
   //do something else...
}

Assuming that you specified button5 as a parameter to indicate that this executes in response to button5 being pressed.

Garrett H
A: 

Can you create a simple structure that contains both the UIButton and the sender and use that?

struct myObject { UIButton* button5; id sender; }

...or, you could create your own NSObject (probably more cocoa-y):

@instance myObject : NSObject { ... }

Dan
Nope. Target/action has a very specific pattern; one argument, the sender.
bbum
A: 

The first parameter to an action is always the sender (you can specify the type and name as appropriate).

If a method is the action for a button, then the first parameter will be the button. If that method is the action for several buttons, then the first parameter will allow you to determine which button was tapped (as Leper describes).

What problem are you actually trying to solve?
There are techniques for passing information to the action method. For example, if you have a button that appears on a table view cell and performs the same action for every cell, then in the action method, you would want to be able to determine which cell's button was tapped.

gerry3
The problem I'm having is that I'm trying to use the following code: [(goGOAppDelegate *)[[UIApplication sharedApplication] delegate] goFlip: (id) sender];However, I get an error stating that the 'sender' is undeclared well the only problem is that I can't make the -(IBAction) a sender due to the need of the (UIButton *) button5 to define this part in the IBAction :gorev2 = (button5.tag == BUTTON7)
0SX
If you are calling a method directly, then it does not have to be an IBAction and it does not have to have any parameters. Again, you probably need to give more higher level info about what you are trying to do--it does not look like you are taking a typical approach.
gerry3
@unknown: What is button5? Who calls this `-gobutton:` method?
KennyTM
+1  A: 

Ok, first.... IBAction doesn't really mean anything special except to Interface Builder. Basically:

#define IBAction void

So whenever you see IBAction, think "void". The only reason it's there is as a flag to tell Interface Builder that a method is a valid method to connect control actions to. The Objective-C compiler doesn't need to know about it and so it's defined to void since all "action" methods return void.

Second, action methods also have one argument which could be an object of any number of types. Because of this, action methods are supposed to use type id as the type for their argument. That way they can be passed a pointer to any Objective-C object without causing the compiler to generate a type checking error.

So usually actions should work something like this:

- (IBAction)myAction:(id)sender {
    if (sender == self.someButton) {
        UIButton *button = (UIButton *)sender;
        ...
        return;
    } else if (sender == self.someControl) {
        UIControl *control = (UIControl *)sender;
        ...
        return;
    }
}

In other words, an id is almost like an untyped pointer like a void * is routinely used in C when some function needs to take a pointer to something of unknown type. sender could be different types of control, so something generic like id is used then sender is cast to something more specific once the code knows what it is.

Anyway, there is absolutely no reason to define something as having a return type of IBAction unless you are going to use that method as a target action in Interface Builder. Having an IBAction in your app delegate seems kind of unusual....

Nimrod
A: 

How can I get the id of the sender before the user touches the control?

Found it! Set a tag and the use viewWithTag.

danselig