子类标签类不符合键值编码

时间:2013-06-17 14:26:57

标签: objective-c subclass

编辑1

This question is answered here对不起噪音。

编辑1

编辑0

我在下面添加了更多代码,并且还显示了我得到的结果的结果图像,这与我的没有子类的结果相同,只有普通标签。在前3行中,标签似乎完全覆盖了按钮,在第4行中没有标签,并注意到按钮#2在第4行缩进。发生了什么事。

此代码仅适用于四行中的第一行,但其他3行类似。

buttons should go from A ... 2 in 4 rows with a spade symbol label between the buttons on the first row

self.cardList = [NSMutableArray arrayWithCapacity:0];
self.yHonorsOrigin = 100;
self.xHonorsOrigin = 100;
self.xHonorsStep = 40.0;
self.xHonorsCurrent = self.xHonorsOrigin;
for( int x=0;x<[cards length]; x++ ){
    [cards substringWithRange:NSMakeRange(x,1)];
    UIButton *b= [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.xHonorsCurrent += self.xHonorsStep;
    [b setTitle:[cards substringWithRange:NSMakeRange(x,1)] forState:UIControlStateNormal];
    [b setTitle:@" " forState:UIControlStateDisabled];
    [b setFrame:CGRectMake(self.xHonorsCurrent, self.yHonorsOrigin, 20, 20)];
    [b setEnabled:YES];
    [b setUserInteractionEnabled:YES];
    [self.view addSubview:b];
    [b addTarget:self action:@selector(spadeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

            self.xHonorsCurrent = self.xHonorsOrigin + self.xHonorsStep/2;
            for( int x=0;x<[cards length]-1; x++ ){
                self.xHonorsCurrent += self.xHonorsStep;
                UILabel *lab = [self valueForKey:@"theSuit" ];
                lab.text = @"\u2660";
                //lab.tintColor = [UIColor clearColor];
                lab.center = CGPointMake(self.xHonorsCurrent, self.yHonorsOrigin);
                [self.view addSubview:lab];
            }
        }
    }

编辑0

我试图使用BDBoundedLabel对UILabel类进行子类化,但是抛出了一个异常,即“子类标签类与键theSuit不符合键值编码。”请帮忙。

BDBoundedLabel.m

#import "BDBoundedLabel.h"

@implementation BDBoundedLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

- (void)drawTextInRect:(CGRect)rect{
    CGContextRef context=UIGraphicsGetCurrentContext();
    CGContextStrokeRect(context, CGRectInset(self.bounds, 1, 1));
    [super drawTextInRect:CGRectInset(rect, 5.0, 5.0)];
}


@end

viewController.h

#import <UIKit/UIKit.h>
#import "BDViewController.h"
#import "BDBoundedLabel.h"

@interface BDnameViewController : UIViewController{
    IBOutlet BDBoundedLabel* theSuit;
}

viewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.cardList = [NSMutableArray arrayWithCapacity:0];
    self.yHonorsOrigin = 100;
    self.xHonorsOrigin = 100;
    self.xHonorsStep = 40.0;
    self.xHonorsCurrent = self.xHonorsOrigin;
    // Do any additional setup after loading the view.

        self.xHonorsCurrent = self.xHonorsOrigin + self.xHonorsStep/2;
        for( int x=0;x<[cards length]-1; x++ ){
            self.xHonorsCurrent += self.xHonorsStep;
            UILabel *lab = [BDBoundedLabel valueForKey:@"theSuit" ];
            lab.text = @"\u2660";
            lab.center = CGPointMake(self.xHonorsCurrent, self.yHonorsOrigin);
            [self.view addSubview:lab];
        }
    }

2 个答案:

答案 0 :(得分:1)

setValue:forKey: valueForKey:将使用访问器方法(setter和getter),因此您需要创建它们。声明属性:

@property(nonatomic,assign) BDBoundedLabel* theSuit; 

另外,就像本杰明所说的那样,你在一个类对象上调用 valueForKey:,也许你打算用 self 作为目标调用这个方法,这样:

UILabel *lab = [self valueForKey:@"theSuit" ];
// Equivalent of lab= self.theSuit

答案 1 :(得分:0)

您的代码没有任何意义。您正在对类对象执行KVO,这几乎从未完成。对于大多数情况,KVO只用于类的实例,这不是你在这里做的。

请尝试澄清您的目的。