tags:

views:

3844

answers:

6

I have the following code :

[replyAllBtn addTarget:self.target action:@selector(ReplyAll:) forControlEvents:UIControlEventTouchUpInside];

  • (void)replyAll:(NSInteger)tid { // some code }

i need to send a parameter to the ReplyAll function .

I Have found no examples for this

As i am new to objective C and iphone programming , any help will be appreciated.

Thanx in advance

A: 

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.

Thomas Tempelmann
+2  A: 

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

MLefrancois
Hii, Thanx for the prompt reply , one doubt tht i have is , since the control is an instance of UIButton , how can i add any property to it ??? May be the question is to naive ... but i dont really get it .
Vijayeta
A: 

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
Dan Keen
+5  A: 

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;
    //...
Rob Fonseca-Ensor
hey thanx this is exactly wht i wanted ...... adding the tag works fine , but in the Controller , where the replyAll func is defined {NSInteger *tid = (*NSInteger)sender.tag;}gives an error saying "request for member tag in something not a structure or union " :(
Vijayeta
Thanks for clearing this up, Rob. I was trying to figure out exactly how I was going to accomplish the same behavior Vijayeta was seeking. @Vijayeta: The sender is the button itself, so I believe you need to try "( ( UIControl * )sender ).tag;". Note that you have to enclose the cast object in parentheses before you can access the tag property.
LucasTizma
+2  A: 

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;
lostInTransit
i have tried tht itself but the error is not regarding the object type , it is not geting the type of the sender . thats y the error says "request for member tag in something not a structure or union"
Vijayeta
Is the sender an object of a custom class you've declared? In that case try [sender tag] instead of sender.tag. That usually works.
lostInTransit
yes , have tried it and it wrks .. thanx
Vijayeta
+2  A: 

Heyyy thanx to all who helped me out .

Its working now :D.

{ NSInteger tid = [sender tag]; }

Vijayeta