简体   繁体   中英

UILabels not showing in subclass of UIView

I have a custom view (subclass of UIView) in which I want to show a UIImageView and several UILabels. The imageView comes asynchronously from FaceBook and the labels get their text from specific methods. The issue is that even though the imageView is rendered successfully when it arrives the labels are not shown. Let me show you the code

@interface CustomView : UIView {

    UIImageView *imageView;
    UILabel *lbl1;
    UILabel *lbl2;
    UILabel *lbl3;
    UILabel *lbl4;
}

@property(nonatomic,retain) UIImageView *imageView;
@property(nonatomic,retain) UILabel *lbl1;
@property(nonatomic,retain) UILabel *lbl2;
@property(nonatomic,retain) UILabel *lbl3;
@property(nonatomic,retain) UILabel *lbl4;

And the implementation is as follows:

@implementation CustomView

@synthesize imageView;
@synthesize lbl1;
@synthesize lbl2;
@synthesize lbl3;
@synthesize lbl4;

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
    {
        self.lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(65, 356, 98, 13)];
        self.lbl1.backgroundColor = [UIColor clearColor];
        [self addSubview:self.lbl1];

        self.lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(260, 356, 50, 13)];
        self.lbl2.backgroundColor = [UIColor clearColor];
        [self addSubview:self.lbl2];

        self.lbl3 = [[UILabel alloc] initWithFrame:CGRectMake(65, 374, 92, 13)];
        self.lbl3.backgroundColor = [UIColor clearColor];
        [self addSubview:self.lbl3];

        self.lbl4 = [[UILabel alloc] initWithFrame:CGRectMake(260, 374, 49, 13)];
        self.lbl4.backgroundColor = [UIColor clearColor];
        [self addSubview:self.lbl4];
    }
    return self;
}

Please note that label rectangles are hardcoded for convenience and do not match. A sample for the method to set label text is the following:

- (void)showLbl1:(NSString *)str withFont:(UIFont *)font andColor:(UIColor *)color
{
    self.lbl1.font = font;
    self.lbl1.textColor = [UIColor cyanColor];
    [self.lbl1 setText:str];
}

The image is delivered with a method that runs by performSelectorInBackground and drawn with a method that runs by performSelectorOnMainThread. Finally, the whole view is added by addSubView in the superView.

Thanx in advance

尝试绘制标签边框并查看它们的位置...。还要检查那里的超级泄漏,您有一个alloc初始化,并且从不释放标签,也没有使用setter,所以您正在执行双分配init。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM