How to add (id) sender to the following -(IBAction)? - iphone

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)

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.

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....

It's not clear what you are trying to do but most actions look like:
- (IBAction) gobutton: (id)sender;

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.

How can I get the id of the sender before the user touches the control?
Found it! Set a tag and the use viewWithTag.

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
{
...
}

Related

How to Pass parameters in IBAction?

I am accessing IBAction programatically & want to pass two parameter with this IBAction call.
Can any one suggest easy way...
IBActions are usually called by user interface elements, and they can't have an arbitrary number of parameters.
If you want to call the action method programmatically, you could abuse the sender parameter by passing a dictionary as an argument, holding the actual arguments you want to pass, like so:
- (void) foo
{
[self myAction: [NSDictionary dictionaryWithObject: #"bar" forKey: #"baz"]];
}
However, I would recommend creating an additional method with two parameters; the IBAction can call it with arguments appropriate to the sender, and programmatically you can call it using whatever arguments you need. This would be a possible outline for the code:
// The atual "logic" method, doing sth interesting
- (void) foo: (NSString *) s bar: (NSInteger) i
{
// some code
}
- (IBAction) myAction: (id) sender
{
// can be invoked by a button, or any view action
if (sender == self.buttonX) {
[self foo: #"x" bar: 42];
}
if (sender == self.buttonY) {
[self foo: #"y" bar: 4];
}
}
- (void) methodCallingFooBarProgrammatically
{
[self foo: #"s" bar: 17];
}
You can pass an array in the IBAction method like this:
-(IBAction)method:(id)sender
{
[sender objectAtIndex:0];
}
or you can do it like this:
-(IBAction)methodName:(NSString *)stringName:(NSString*)stringName2
{
// You can pass an array and even a dictionary
}
IBAction method follow a spesific pattern either
- (IBAction)action:(id)sender;
or
- (IBAction)action:(id)sender forEvent:(UIEvent *)event;
where sender is the UI object that sends the event, and event being the UIEvent itself.
If you are not sending these arguments then you don't want an IBAction method. Define a normal method that takes the two arguments you want and if you IBAction methods need to call it as well then do that. IBAction methods are defined as IBAction so that interface builder can find them in your code, so there is no reason to define an IBAction method that does not follow the pattern above.
the IBAction methods could receive two parameters about the sender object and the touch event, you cannot "pass" anything, you can receive only these via:
- (IBAction)action
- (IBAction)action:(id)sender
- (IBAction)action:(id)sender forEvent:(UIEvent *)event
you could use only the sender's tag property to pass a custom identifier as NSInteger.
HERE IS THE POINT
everything else what you would like to "pass" must be exists on your Model layer already! if you know what it is...
therefore, you can reach your datas from the Model layer after you receive the action.

In a UIButton method is there a way to check if SENDER is a subclass of a specific type?

Basically if I have a method declared and tied to a button such as:
- (IBAction) clickMe:(id)sender {
.....
}
Is there a way in this method to see whether sender is a subclass of type UIViewController and if so, somehow access something like [sender.view removeFromSuperView] ?
Is this at all possible?
Thanks!
You can use
isKindOfClass:
Returns a Boolean value that indicates
whether the receiver is an instance of
given class or an instance of any
class that inherits from that class.
(required)
to determine if sender is of a certain subclass. After that you should cast sender to UIViewController to remove it's view, else you'll get a warning from the compiler.

How to know or retrieve the sender id

I have the following method
-(IBAction)back:(id)sender {
}
and would like to be able to know the sender id.
e.g. if there are multiple buttons linked to this method, I would like to know which button was pressed.
Simply use the tag property, inherited from UIView, which is a NSInteger, in a switch statement, or using if conditions.
The tag property can be set in your code, or through InterfaceBuilder.
[sender tag]
I don't know what you mean by "id" (the "sender" is an id, effectively an NSObject *), but you could use tags. You have to set the tag beforehand in Interface Builder or programmatically.
If you have set up IBOutlets for the buttons in your interface then you can simply compare the sender to those.
That is in your interface definition if you have
...
(IBOutlet) UIButton *button1;
(IBOutlet) UIButton *button2;
...
and in your implementation you have:
- (IBAction) buttonPressed: (id) sender
{
if (sender == button1) {
....
}
else if (sender == button2) {
...
}
}
Personally, I'd prefer to use different action methods for each button and then they can all call a common routine for the things that are common. However, for simple projects the above will work.
-J
Set the tag property of each button to a unique integer (either in IB or programmatically) and switch on it inside your action method.

How do I determine which control fired an event?

I have the Value Changed event of two UISliders (both of which have referencing outlets) wired up to the following method:
-(IBAction) sliderMoved:(id) sender {}
How can I determine which slider was moved so that I can get its value and update the corresponding label? Or would it be simpler to have two separate events, one for each slider? The second option seems like unnecessary replication to me.
Cheers,
Dan
It's going to be the sender variable. Just do all your work with it.
It's legal to strongly type it, by the way. So if you know you're only going to deal with UISlider objects, you can do -(IBAction)someAction:(UISlider*)slider {}.
You can use [sender tag] to get the tag of the slider if you have set that up. Assign the tag when you create the sliders or in interface builder.
-(IBAction) sliderMoved:(UISlider*)sender {
switch ( [sender tag] ) {
case kMyOneSlider: ... break;
case kMyOtherSlider: ... break;
}
}
You can use == with the outlet members for each slider:
-(IBAction) sliderMoved:(UISlider*)sender {
if ( sender == mOneSlider ) ...;
if ( sender == mOtherSlider ) ...;
}
Or you can set up different actions for each slider. I generally share one action method if there is some common code in the handlers.

How can I pass a parameter to this function?

I have the following code:
[replyAllBtn addTarget:self.target action:#selector(ReplyAll:) forControlEvents:UIControlEventTouchUpInside];
- (void)replyAll:(NSInteger)tid {
// some code
}
How can I send a parameter to the ReplyAll function?
The replyAll method should accept (id)sender. If a UIButton fired the event, then that same UIButton will be passed as the sender. UIButton has a property "tag" that you can attach your own custom data to (much like .net winforms).
So you'd hook up your event with:
[replyAllBtn addTarget:self.target action:#selector(ReplyAll:) forControlEvents:UIControlEventTouchUpInside];
replyAllBtn.tag=15;
then handle it with:
(void) ReplyAll:(id)sender{
NSInteger *tid = ((UIControl*)sender).tag;
//...
A selector function will normally be defined as such:
- (void) ReplyAll:(id)sender;
So the only parameter an action will ever receives is the actual control that called it.
You could just add a property to your control that can be read in replyAll
If you want to send an int value, set the tag of the button = the int value you want to pass. Then you can access the tag value of the button to get the int you wanted.
NSInteger is not a pointer. Try this
NSInteger tid = sender.tag;
It's working now :D.
{
NSInteger tid = [sender tag];
}
The MVC model used in Cocoa works differently. Basically, the idea is that a control (=view) such as a button only lets a function know it was pressed, not knowing what this means. The function then has to know all the dynamics and dependencies. In your case, it's the function that has to find the parameter. To accomplish that, you'll "bind" other objects to the function (= controller).
I suggest you read a few Cocoa tutorials first if you want to get ahead with iPhone programming.
There's a few good ways to do this. The two most commonly implemented would be to have the controller (who's receiving the action) know about possible senders, or having the sender itself have a method that you end up using to determine the proper behavior.
The first (my preferable way, but it's easy to argue the opposite) would be implemented like such:
#interface Controller : NSObject {
UIButton *_replyToSender;
UIButton *_replyToAll;
}
- (void)buttonClicked:(id)sender;
#end
#implementation Controller
- (void)buttonClicked:(id)sender {
if (sender == _replyToSender) {
// reply to sender...
} else if (sender == _replyToAll) {
// reply to all...
}
}
#end
The second way would be implemented in a manner such as:
typedef enum {
ReplyButtonTypeSender = 1,
ReplyButtonTypeAll,
} ReplyButtonType;
#interface Controller : NSObject {
}
- (void)buttonClicked:(id)sender;
#end
#interface MyButton : UIButton {
}
- (ReplyButtonType)typeOfReply;
#end
#implementation Controller
- (void)buttonClicked:(id)sender {
// You aren't actually assured that sender is a MyButton, so the safest thing
// to do here is to check that it is one.
if ([sender isKindOfClass:[MyButton class]]) {
switch ([sender typeOfReply]) {
case ReplyButtonTypeSender:
// reply to sender...
break;
case ReplyButtonTypeAll:
// reply to all...
break;
}
}
}
#end