0 cocoa cocoa-touch subclass objective-c uibutton
我创建了一个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
Run Code Online (Sandbox Code Playgroud)
我想我可以通过执行以下操作创建此对象并设置注释对象:
MyDetailButton* rightButton = [MyDetailButton buttonWithType:UIButtonTypeDetailDisclosure];
rightButton.annotation = localAnnotation;
Run Code Online (Sandbox Code Playgroud)
localAnnotation是一个NSObject,但它实际上是一个MKAnnotation.我不明白为什么这不起作用,但在运行时我得到这个错误:
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'
Run Code Online (Sandbox Code Playgroud)
"
我不明白为什么它甚至会查看UIButton,因为我已经将其子类化了,所以应该查看MyDetailButton类来设置该注释属性.我错过了一些非常明显的东西.感觉就像:)
提前感谢您提供的任何帮助
罗斯
n8g*_*ray 14
UIButton是一个类集群,这意味着Apple的实现buttonWithType:可能看起来像这样:
+(id)buttonWithType:(UIButtonType)t {
switch (t) {
case UIButtonTypeDetailDisclosure:
return [[[PrivateDetailDisclosureButtonClass alloc] init] autorelease];
case ...
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当你打电话给[MyDetailButton buttonWithType:UIButtonTypeDetailDisclosure];你时,你没有得到一个实例MyDetailButton,你会得到一个实例PrivateDetailDisclosureButtonClass(或者Apple实际上称之为的实例).
但是请注意,你可以得到buttonWithType,如果你把它实例化一个子类UIButtonTypeCustom(至少在模拟器中运行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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4056 次 |
| 最近记录: |