子类化UIButton但不能访问我的属性 [英] Subclassing UIButton but can't access my properties

查看:83
本文介绍了子类化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
//

$ b b #importMyDetailButton.h


@implementation MyDetailButton


@synthesize注释;

@end

我想我可以创建这个对象并设置注释对象通过执行以下操作:

  MyDetailButton * rightButton = [MyDetailButton buttonWithType:UIButtonTypeDetailDisclosure]; 
rightButton.annotation = localAnnotation;

localAnnotation是一个NSObject,但它实际上是一个MKAnnotation。我不明白为什么这不工作,但在运行时我得到这个错误:

  2010-05-27 10: 37:29.214 DonorMapProto1 [5241:207] ***  -  [UIButton注释]:发送到实例0x445a190的无法识别的选择器
2010-05-27 10:37:29.215 DonorMapProto1 [5241:207] ***终止应用to uncaught exception'NSInvalidArgumentException',reason:'*** - [UIButton annotation]:unrecognized selector sent to instance 0x445a190'



'



我不明白为什么它甚至看着UIButton,因为我已经子类化了所以它应该查看MyDetailButton类来设置注释属性。我错过了一些很明显的东西。 )



感谢您提供任何帮助,您可以提供



Ross

解决方案

UIButton是一个类集群,这意味着Apple的实现 buttonWithType:像这样:

  +(id)buttonWithType:(UIButtonType)t {
switch(t){
case UIButtonTypeDetailDisclosure:
return [[[PrivateDetailDisclosureButtonClass alloc] init] autorelease];
case ...
}
}

调用 [MyDetailButton buttonWithType:UIButtonTypeDetailDisclosure]; 您没有得到 MyDetailButton 的实例, code> PrivateDetailDisclosureButtonClass (或任何苹果实际调用它)。



但请注意, >如果你用 UIButtonTypeCustom (至少在运行v3.0的模拟器中)调用它,就可以实例化 buttonWithType

  // LGButton是UIButton的一个简单的子类
LGButton * testBtn = [LGButton buttonWithType:UIButtonTypeCustom];
LGButton * testBtn2 = [LGButton buttonWithType:UIButtonTypeDetailDisclosure];
NSLog(@testBtn:%@,testBtn2:%@,[testBtn class],[testBtn2 class]);
//输出:testBtn:LGButton,testBtn2: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 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'

'

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

Ross

解决方案

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 ...
  }
}

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).

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但不能访问我的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆