0

编辑 1

这个问题在这里得到了回答抱歉打扰了。

编辑 1

编辑 0

我在下面包含了更多代码以及结果图像,它显示了我得到的结果,这与我在没有子类化的情况下得到的结果相同,只有普通标签。在前 3 行中,标签似乎完全覆盖了按钮,而在第 4 行中没有标签,请注意按钮 #2 在第 4 行缩进。这是怎么回事。

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

按钮应该从 A ... 2 开始,共 4 行,第一行按钮之间有一个黑桃符号标签

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

我曾尝试使用 对 UILabel 类进行子类化BDBoundedLabel,但抛出了一个异常,即“子类标签类不符合 key 的键值编码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

视图控制器.h

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

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

视图控制器.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];
        }
    }
4

2 回答 2

1

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

@property(nonatomic,assign) BDBoundedLabel* theSuit; 

此外,就像本杰明所说,您正在调用valueForKey:在类对象上,也许您打算以self作为目标调用该方法,这样:

UILabel *lab = [self valueForKey:@"theSuit" ];
// Equivalent of lab= self.theSuit
于 2013-06-17T14:30:25.250 回答
0

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

请尝试澄清您要执行的操作。

于 2013-06-17T15:45:27.640 回答