пользовательский UIButton, неопознанный селектор отправлен в экземпляр

Я пытаюсь создать подкласс UIButton, чтобы присоединить к нему свойство, но, как вы уже могли догадаться, я получаю ошибку «неопознанный селектор отправлен в экземпляр».

Точная ошибка: [UIButton setAnnotPin:]: нераспознанный селектор отправлен в экземпляр

Вот важные части кода:

MapViewController.h:

  #import "UIRightPinAccessoryButton.h"

MapViewController.m:

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

UIRightPinAccessoryButton *rightAccessoryButton = [UIRightPinAccessoryButton buttonWithType:UIButtonTypeDetailDisclosure];
rightAccessoryButton.frame = CGRectMake(0, 0, 35, 35);
rightAccessoryButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
rightAccessoryButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[rightAccessoryButton addTarget:self action:@selector(rightAccessoryButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 

rightAccessoryButton.annotPin = annot;  **// Error comes here**
}

и вот мой класс UIRightPinAccessoryButton:

UIRightPinAccessoryButton.h

#import "Annotations.h"

@interface UIRightPinAccessoryButton : UIButton{
  Annotations *annotPin;
} 

@property (nonatomic) Annotations *annotPin;

UIRightPinAccessoryButton.m

#import "UIRightPinAccessoryButton.h"

@implementation UIRightPinAccessoryButton

@synthesize annotPin;

@end

Я действительно не понимаю, почему xcode ищет метод setAnnotPin: в UIButton вместо моего пользовательского UIRightPinAccessoryButton ??


person Kito    schedule 09.06.2012    source источник


Ответы (1)


Этот раздел [UIRightPinAccessoryButton buttonWithType:UIButtonTypeDetailDisclosure];

Не будет возвращать объект типа UIRightPinAccessoryButton, но он вернет объект типа UIButton, поэтому измените его на

UIRightPinAccessoryButton *rightAccessoryButton = [[UIRightPinAccessoryButton alloc] init];

Также кажется, что нет способа изменить тип кнопки на UIButtonTypeDetailDisclosure после создания подкласса, вы можете установить свое собственное изображение, чтобы оно выглядело так же, хотя
Также вы не можете легко создать подкласс buttonWithType, пожалуйста, прочтите больше в iPhone: переопределите UIButton buttonWithType для возврата подкласса

person Omar Abdelhafith    schedule 09.06.2012