gpt4 book ai didi

ios - 自定义 UIButton,发送到实例的无法识别的选择器

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:07:15 26 4
gpt4 key购买 nike

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

最佳答案

本节[UIRightPinAccessoryButton buttonWithType:UIButtonTypeDetailDisclosure];

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

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

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

关于ios - 自定义 UIButton,发送到实例的无法识别的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10962252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com