-3

我有以下课程

#import <UIKit/UIKit.h>

@interface UIImageViewInfo : UIImageView
@property(nonatomic,strong) NSString* colorInfo;
@end

在 function1 中创建 UIImageViewInfo 时(下面的帖子)我得到了正确的结果

“UIImageViewInfo:0x1d8acd60;baseClass = UIImageView;帧 =(0 0;20 20);不透明 = NO;userInteractionEnabled = NO;标签 = 2000;层 = CALayer:0x1d8a0560>>”

但是在function2中创建一些东西(发布在下面)我得到了-(null)

UIImageViewInfo: 0x8372c40; 基类 = UIImageView; 帧 = (0 0; 20 20); 阿尔法 = 0; 隐藏=是;不透明=否;用户交互启用 = 否;标签 = 2000; 层 = CALayer: 0x8380090>> - (null)

为什么有 - (null) ?

提前感谢

PS> 这里更多来自我的代码

-(void)function1{
UIImageViewInfo *image;
self.solutionPinSlots = [[NSMutableArray alloc] init];

for (int i=0; i<M; i++) {
    image = [[UIImageViewInfo alloc] initWithImage:[UIImage imageNamed:@"solutionPeg@2x.png"]];
    image.translatesAutoresizingMaskIntoConstraints=NO;

    image.hidden=YES;
    image.alpha = 0;
    image.colorInfo = @"clear";

    image.tag = 2000*(self.brainController.slotsIndex+1) +i;
    [self.solutionPinSlots addObject:image];
    [self.view addSubview:(UIImageViewInfo *)self.solutionPinSlots[i]];
}
NSLog(@"solutionSlots: %@",self.solutionPinSlots);
__block int counter = 0;
[self.brainController.solution enumerateObjectsUsingBlock:^(NSString *peg, NSUInteger idx, BOOL *stop){

    for (int i=0;i<M;i++) {
        if ([self.brainController.slots[self.brainController.slotsIndex][i] isEqual:peg]) {

            if (i==idx) {
             ((UIImageViewInfo *) self.solutionPinSlots[counter]).backgroundColor = [UIColor whiteColor];
              ((UIImageViewInfo *) self.solutionPinSlots[counter]).colorInfo=@"white";
            }else{
                ((UIImageViewInfo *) self.solutionPinSlots[counter]).backgroundColor = [UIColor blackColor];
                ((UIImageViewInfo *) self.solutionPinSlots[counter]).colorInfo=@"black";}
            ((UIImageViewInfo *) self.solutionPinSlots[counter]).hidden=NO;
            ((UIImageViewInfo *) self.solutionPinSlots[counter++]).alpha=1;
        }
    }

}];

[self.solutionPinSlots shuffle];

[self updateSolutionPinSlots];

}

-(void)function2{
NSString *obj;


for (int idx=0; idx< M;idx++ ) {
    UIImageViewInfo *image = [[UIImageViewInfo alloc] initWithImage:[UIImage imageNamed:@"solutionPeg@2x.png"]];
    image.translatesAutoresizingMaskIntoConstraints=NO;
    obj = (NSString *) [self.solutionPinSlots objectAtIndex:idx];
    image.tag=2000*(self.brainController.slotsIndex+1)+idx;
    image.hidden=NO;
    image.alpha = 1;
    if ([obj isEqualToString:@"clear"]) {
        image.hidden=YES;
        image.alpha = 0;
        image.colorInfo=@"clear";
        image.backgroundColor=[UIColor clearColor];
    }else if ([obj isEqualToString:@"white"]){
        image.colorInfo=@"white";
        image.backgroundColor=[UIColor whiteColor];
    }else if ([obj isEqualToString:@"black"]){
        image.colorInfo=@"black";
        image.backgroundColor=[UIColor blackColor];
    }else{
        NSLog(@"Something is Wrong!!!");
    }

    [self.solutionPinSlots replaceObjectAtIndex:idx withObject:image];

    [self.view addSubview:image];
}

}

4

2 回答 2

2

没关系。它来自descriptionUIView 的方法,该方法相当复杂,并且会转储大量有关视图的信息。其中一个条件被您的配置绊倒并导致(null)最后。

安全忽略。

UIKit中没有UIImageViewInfo类,所以实现必须在你的项目中。在该实现中,可能有一种description方法可以实现,例如:

- (NSString*)description {
    ....
    return [NSString stringWithFormat:@"UIImageViewInfo:%p; %@ - %@", self, [super description], someInstanceVariableThatHappensToBeNil]];
} 

只有你description的可能更复杂,因为有时会这样做有时会输出。

并不是说UI在子类化 UIKit 时不应该为类添加前缀。

于 2013-04-28T18:33:41.587 回答
1

这是因为您的 NSLog 调用(您没有显示)正在请求第二个对象,而第二个对象为空。

于 2013-04-28T17:56:34.020 回答