【问题标题】:custom UIButton, unrecognized selector sent to instance自定义 UIButton,无法识别的选择器发送到实例
【发布时间】:2012-06-13 07:18:13
【问题描述】:

我正在尝试子类化 UIButton 以将属性附加到它,但您可能已经猜到我收到“无法识别的选择器发送到实例”错误。

确切的错误:[UIButton setAnnotPin:]: unrecognized selector sent to instance

以下是代码的重要部分:

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 在 UIButton 上寻找 setAnnotPin: 方法而不是我的自定义 UIRightPinAccessoryButton?

【问题讨论】:

    标签: ios xcode unrecognized-selector


    【解决方案1】:

    本栏目[UIRightPinAccessoryButton buttonWithType:UIButtonTypeDetailDisclosure];

    不会返回 UIRightPinAccessoryButton 类型的对象,但会返回 UIButton 类型的对象 所以改成

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

    此外,一旦按钮的子类化,似乎没有办法将按钮的类型更改为 UIButtonTypeDetailDisclosure,您可以设置自己的图像以使其看起来相同
    您也不能轻松地继承 buttonWithType,请阅读更多来自iPhone: Override UIButton buttonWithType to return subclass

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-21
      • 2021-04-27
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多