2012-11-08 92 views
0

我創建了一個CustomButton爲什麼我需要在@selector中傳遞參數。自定義按鈕是註解視圖的一部分。在CustomButton中,我把它作爲屬性UIButtonType,但是在輸出中沒有任何東西出來。輸出是一個沒有任何東西的按鈕,當我想要打開一個視圖控制器時,當我在註解內部觸及時消失。類型UIButtonTypeInfoLight的自定義按鈕

這是CustomButton.m在CustomButton.h

代碼
@interface CustomButton : UIButton{ 

} 

@property (nonatomic, strong)NSString * name; 
@property (nonatomic, assign)UIButtonType typeButton; 
@end 

@implementation CustomButton.h 

@synthesize name; 
@synthesize buttonType; 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 

     self.typeButton = UIButtonTypeInfoLight; 

    } 
return self; 
} 
在MapViewController.m

-(void)loadDetailListViewController: (CustomButton *)aName{ 

      //I want to open a viewController 
      //....... 
    } 

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

     MKPinAnnotationView *annotationView = (MKPinAnnotationView*) [mapView 
       dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier]; 


     annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                reuseIdentifier:AnnotationIdentifier]; 

    //......... 

    CustomButton *rightButton = [CustomButton buttonWithType:UIButtonTypeCustom]; 
    [rightButton setName:[NSString stringWithFormat:@"%@", self.nameTable]]; 

    [rightButton addTarget:self action: @selector(loadDetailListViewController:) 
           forControlEvents:UIControlEventTouchUpInside]; 

    annotationView.rightCalloutAccessoryView = rightButton; 
    annotationView.canShowCallout = YES; 
    annotationView.draggable = YES; 
    return annotationView; 
    } 

爲什麼當我觸摸了註解內消失

+0

如果您只想在loadDetailListViewController方法中訪問一些與註釋相關的信息,則可以使用常規的UIButton並使用calloutAccessoryControlTapped委託方法來訪問view.annotation。 – Anna

+0

我如何?你給我看一個例子嗎? –

+1

見http://stackoverflow.com/questions/4565197/how-to-find-which-annotation-send-showdetails和http://stackoverflow.com/questions/7334068/show-another-view-when-map-註解的是,點擊。 – Anna

回答

1

請不要按照文檔的說明:

buttonWithType:創建並返回指定 類型的新按鈕。

  • (ID)buttonWithType:(UIButtonType)按鈕類型

參數

按鈕類型的按鈕式。請參閱UIButtonType以瞭解可能的值。

返回值

新創建的按鈕。

討論此方法是一個方便的構造函數,用於創建具有特定配置的 按鈕對象。 它是你的子類UIButton, 這個方法不會返回你的子類的一個實例。如果你想 創建特定的子類的實例,你必須分配/初始化直接 按鈕。

創建當自定義按鈕,其與按鈕的 類型UIButtonTypeCustom-的幀被設置爲(0,0,0,0) 最初的按鈕。在將按鈕添加到界面之前,應該將框架更新爲更合適的值。

你的代碼工作不知何故,當您在使用buttonWithType:UIButtonTypeCustom,如果有人改變它(喜歡UIButtonTypeRoundedRect)不好的事情會發生。

因爲你沒有在代碼中的任何地方使用CustomButton -initWithFrame:,但提供了它的實現,我可以建議你想使用它作爲所需的初始化,這是很好的。