-1

I have a button added to an annotation marker for a map view page in my iphone App

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
                action:@selector(go_to_detail_page:)    
      forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = rightButton; 

and my receiving function is

-(IBAction)go_to_detail_page:(id)sender;{
}

My question is the following. I am creating quite a lot of markers on my page and I would like to pass a unique identifier when that particular annotation view button is pressed, even a string would be fine. How can I pass a string to the go_to_detail_page method once the annotation is pressed?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
user1096447
  • 429
  • 1
  • 7
  • 22
  • Why don't you use "sender" for identification? The button passes in itself as the argument of its action selector. –  Dec 31 '12 at 11:21
  • sorry how do i do that? thanks – user1096447 Dec 31 '12 at 11:25
  • 1
    @user1096447 : please use good naming conventions...camelCase for method names. – Anoop Vaidya Dec 31 '12 at 11:31
  • 1
    You don't need to use IBAction. Put void instead. IBAction or IBOutlet are intended to work with IB (Interface Builder). They are just placeholders. Under the hood they mean void. – Lorenzo B Dec 31 '12 at 11:42

4 Answers4

1

Use rightButton.tag = 1 and in

-(IBAction)go_to_detail_page:(id)sender{
    UIButton *button = (UIButton *)sender;
    if(button.tag==1){//this is the rightButton
         //your logic goes here

    }
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
Nikita P
  • 4,226
  • 5
  • 31
  • 55
0

Hey you can sub class the UIButton and put NSString* member to tag each button instance..

Tony Thomas
  • 967
  • 10
  • 20
  • Haha... this is like, going to venus and borrowing carbon-monoxide from there... – Anoop Vaidya Dec 31 '12 at 11:32
  • oh so whats ur best way to produce CO ? Let me study too..Vaidyarae – Tony Thomas Dec 31 '12 at 11:37
  • 1
    UIButton contains something called tag, you can use it, no need to subclass and adding another ivar to it. – Anoop Vaidya Dec 31 '12 at 11:39
  • 1
    Sub classing standard controls to contain contextual data is a well accepted practice.If you have got numerous dynamically allocated objects, thats wat I tried to convey Anoop Vaidya saab! lol – Tony Thomas Dec 31 '12 at 11:41
  • tag an integer only, but for making the concepts clear and more extensible , its always good to contain objects (composite) ... – Tony Thomas Dec 31 '12 at 11:44
  • 1
    I would prefer to use associative references in this case. – Lorenzo B Dec 31 '12 at 11:44
  • 1
    @AnoopVaidya Subclassing controls for storing information is fine. You can't fit an entire database in a simple integer tag. –  Dec 31 '12 at 12:13
  • I agree, but no need to subclass, he can pass 0,1,2, etc and in the action method he can track the data that he want to send. The data would be saved either in dict or array or xml etc... – Anoop Vaidya Dec 31 '12 at 12:21
  • thats the design flow of Anoop Vaidya ! while you have an array of buttons, u keep another array to store its context!!!and what if you want sort these buttons ? will u sort the corresponding context array too?...bad bad design !!! – Tony Thomas Dec 31 '12 at 13:14
0

You can set an unique identifier as button tag

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
                action:@selector(go_to_detail_page:)    
      forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = rightButton; 
rightButton.tag == any unique number // it would act as unique identifier

and retrieve it as follows

- (IBAction)go_to_detail_page:(id)sender;{

    UIButton *button = (UIButton *)sender;
    if(button.tag==unique identifier){
        // this is the rightButton
        // your logic
    }
    else
    {

    }
}
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
thavasidurai
  • 1,972
  • 1
  • 26
  • 51
0

In my modest opinion you could have two options.

The first option is to assign to each button a tag and then retrieve it in its action. So, for example, for each button you will assign a different tag.

rightButton.tag = // a tag of integer type

and then you will use like this

- (void)goToDetailedPage:(id)sender
{
    UIButton *senderButton = (UIButton *)sender;

    int row = senderButton.tag;        
    // do what you want with the tag
}

The other option is using associative references. Through them, and without subclassing UIButton, you could just create a property (of type NSString) and use it as an identifier.

To use them, take a look here at Subclass UIButton to add a property.

It's a quite complex concept but through it you have a lot of flexibility.

Notes

You don't need to use IBAction. Put void instead. IBAction or IBOutlet are intended to work with IB (Interface Builder). They are just placeholders. Under the hood they mean void.

Use camel case notation. For example, and as I wrote in my answer, instead of go_to_detail_page, use goToDetailedPage.

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190