2012-12-31 30 views
-1

我有一個按鈕添加到註釋標記爲地圖視圖頁面在我iPhone應用程序傳遞數據的方法,當一個按鈕被竊聽

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

和我的接收功能是

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

我的問題如下。我在頁面上創建了很多標記,當按下特定的註釋視圖按鈕時,我想傳遞一個唯一的標識符,即使是一個字符串也可以。一旦註解被按下,我怎樣才能將一個字符串傳遞給go_to_detail_page方法?

+0

爲什麼不使用「發件人」進行識別?該按鈕本身作爲其動作選擇器的參數傳遞。 – 2012-12-31 11:21:24

+0

對不起,我該怎麼做?謝謝 – user1096447

+1

@ user1096447:請使用良好的命名約定... camelCase用於方法名稱。 –

回答

1

使用​​3210 和

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

    } 
} 
0

嘿,你可以子類的UIButton並提出的NSString *成員來標記每個按鈕實例..

+0

哈哈...這就像是,去維納斯並從那裏借用一氧化碳...... –

+0

哦,那麼你最好的方式來生產CO?讓我學習吧..Vaidyarae –

+1

UIButton包含一些名爲tag的東西,你可以使用它,不需要繼承和添加另一個ivar。 –

0

您可以設置一個唯一的標識符按鈕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 

並檢索它如下

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

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

    } 
} 
0

在我的謙虛看來,你可以有兩種選擇。

第一個選項是分配給每個按鈕一個tag然後檢索它的動作。因此,例如,對於每個按鈕,您將分配一個不同的標籤。

rightButton.tag = // a tag of integer type 

,然後你會使用這樣

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

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

另一種選擇是使用關聯引用。通過它們,並且不需要繼承UIButton,您可以創建一個屬性(類型爲NSString)並將其用作標識符。

要使用它們,請看看Subclass UIButton to add a property

這是一個相當複雜的概念,但通過它你有很大的靈活性。

你並不需要使用IBAction爲。把空白代替。 IBAction或IBOutlet旨在與IB(Interface Builder)一起使用。他們只是佔位符。在引擎蓋下他們意味着無效。

使用駝峯符號表示法。例如,正如我在回答中所寫的,而不是go_to_detail_page,請使用goToDetailedPage