2012-07-23 133 views
1

我在MKAnnotation的標註上有一個detailDisclousure按鈕。當按下這個按鈕時,我需要調用一個傳遞參數的方法來標識調用它的annotation。它怎麼能做到?通過UIButton選擇器上的參數

這是我viewForAnnotation:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 

if ([annotation isKindOfClass:[Annotation class]]) 
{ 
    static NSString* identifier = @"identifier"; 

    MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 

    if (pinView == nil) 
    { 
     pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]autorelease]; 
    } 

    Annotation *antt = annotation; 

    pinView.canShowCallout = YES; 
    pinView.draggable = YES; 

    UIButton* detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

    [detailButton performSelector:@selector(goToViewWithAnnotation:) withObject:antt]; 

    pinView.rightCalloutAccessoryView = detailButton; 

    return pinView; 
} 

else 
{ 
    return nil; 
} 
} 

這是應該使用參數調用的方法:

-(void)goToViewWithAnnotation:(Annotation *)annotation 
{ 
    NextViewController *nextView = [[NextViewController alloc]initWithNibName:@"NextViewController" bundle:nil]; 

nextView.id = annotation.id; 

[self.navigationController nextView animated:YES]; 
} 

回答

4

您只能通過UIButtontag屬性通過任何NSInteger

+0

這樣做的工作。非常感謝=) – 2012-07-23 16:51:10

+0

不客氣。 :) – holex 2012-07-23 17:25:42

2

這是不可能與-addTarget:action:forControlEvent:UIButton發送數據:

[detailButton addTarget:self action:@selector(goToViewWithAnnotation:) forControlEvents:UIControlEventTouchUpInside]; 

試着處理這樣的數據我以其他方式,當你觸發選擇器。就像保存一個指針或var一樣。

UIButton有這些來電者,你可以看到:

- (void)action 
- (void)action:(id)sender 
- (void)action:(id)sender forEvent:(UIEvent *)event 
+0

哦..和順便說一句,可能的答案在這裏:http://stackoverflow.com/questions/5690283/passing-custom-data-in-uibutton-addtarget – NSPunk 2012-07-23 14:02:32

0

我認爲你可以在.h文件中添加屬性(這是(譯註*)註釋)。然後你可以在「 - (void)goToViewWithAnnotation」方法中使用註釋。或者您可以自定義一個新按鈕,繼承UIControl