我创建了一个 UIButton 的子类:
I've created a sub class of UIButton:
//
// DetailButton.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyDetailButton : UIButton {
NSObject *annotation;
}
@property (nonatomic, retain) NSObject *annotation;
@end
//
// DetailButton.m
//
#import "MyDetailButton.h"
@implementation MyDetailButton
@synthesize annotation;
@end
我想我可以通过执行以下操作来创建这个对象并设置注释对象:
I figured that I can then create this object and set the annotation object by doing the following:
MyDetailButton* rightButton = [MyDetailButton buttonWithType:UIButtonTypeDetailDisclosure];
rightButton.annotation = localAnnotation;
localAnnotation 是一个 NSObject 但它实际上是一个 MKAnnotation.我不明白为什么这不起作用,但在运行时我收到此错误:
localAnnotation is an NSObject but it is really an MKAnnotation. I can't see why this doesn't work but at runtime I get this error:
2010-05-27 10:37:29.214 DonorMapProto1[5241:207] *** -[UIButton annotation]: unrecognized selector sent to instance 0x445a190
2010-05-27 10:37:29.215 DonorMapProto1[5241:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIButton annotation]: unrecognized selector sent to instance 0x445a190'
'
我看不出它为什么要查看 UIButton,因为我已经对它进行了子类化,所以它应该查看 MyDetailButton 类来设置该注释属性.我是否错过了一些非常明显的事情.感觉是这样的:)
I can't see why it's even looking at UIButton because I've subclassed that so it should be looking at the MyDetailButton class to set that annotation property. Have I missed something really obvious. It feels like it :)
提前感谢您提供的任何帮助
Thanks in advance for any help you can provide
罗斯
UIButton 是一个类簇,这意味着苹果的 buttonWithType:
实现大概是这样的:
UIButton is a class cluster, which implies that Apple's implementation of buttonWithType:
probably looks something like this:
+(id)buttonWithType:(UIButtonType)t {
switch (t) {
case UIButtonTypeDetailDisclosure:
return [[[PrivateDetailDisclosureButtonClass alloc] init] autorelease];
case ...
}
}
因此,当您调用 [MyDetailButton buttonWithType:UIButtonTypeDetailDisclosure];
时,您不会得到 MyDetailButton
的实例,而是会得到 PrivateDetailDisclosureButtonClass
(或任何苹果实际称呼的).
So when you call [MyDetailButton buttonWithType:UIButtonTypeDetailDisclosure];
you don't get an instance of MyDetailButton
, you get an instance of PrivateDetailDisclosureButtonClass
(or whatever Apple actually calls it).
但是请注意,如果您使用 UIButtonTypeCustom
调用它,您可以让 buttonWithType
实例化一个子类(至少在模拟器运行v3.0):
Note, however, that you can get buttonWithType
to instantiate a subclass if you call it with UIButtonTypeCustom
(At least in the simulator running v3.0):
// LGButton is a straightforward subclass of UIButton
LGButton *testBtn = [LGButton buttonWithType:UIButtonTypeCustom];
LGButton *testBtn2 = [LGButton buttonWithType:UIButtonTypeDetailDisclosure];
NSLog(@"testBtn: %@, testBtn2: %@", [testBtn class], [testBtn2 class]);
// Output: testBtn: LGButton, testBtn2: UIButton
这篇关于子类化 UIButton 但无法访问我的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!