0

@selector でパラメーターとして渡す必要があるのはなぜですか。カスタム ボタンは、annotationView の一部です。CustomButton では、プロパティ UIButtonType として配置しましたが、出力には何も表示されません。出力は何もないボタンで、View Controller を開きたいときに注釈の内側を修正すると消えます。

これは CustomButton.h のコードです。

@interface CustomButton : UIButton{

}

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

CustomButton.m で

@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;
   }

注釈の内側をタッチアップすると消えるのはなぜですか?

4

1 に答える 1

1

ドキュメントの指示に従ってください。

buttonWithType: 指定されたタイプの新しいボタンを作成して返します。

  • (id)buttonWithType:(UIButtonType)buttonType

パラメーター

buttonType ボタンのタイプ。可能な値については、UIButtonType を参照してください。

戻り値

新しく作成されたボタン。

説明 このメソッドは、特定の構成を持つボタン オブジェクトを作成するための便利なコンストラクタです。UIButton をサブクラス化すると、 このメソッドはサブクラスのインスタンスを返しません。特定のサブクラスのインスタンスを作成する場合は、 ボタンを直接割り当て/初期化する必要があります。

カスタム ボタン (UIButtonTypeCustom 型のボタン) を作成する場合、ボタンのフレームは最初に (0, 0, 0, 0) に設定されます。ボタンをインターフェイスに追加する前に、フレームをより適切な値に更新する必要があります。

UIButtonTypeCustomatを使用している間、コードは何らかの形で機能します。buttonWithType:誰かが変更すると ( のように) 悪いことが起こりUIButtonTypeRoundedRectます。

コードのどこにも使用していませんがCustomButton -initWithFrame:、実装が提供されているため、目的の初期化子として使用することをお勧めします。

于 2012-11-08T14:43:42.240 に答える