问题
我有一个UILabel
隐藏在几层子视图中,我似乎无法更改它的文本。更新标签的代码在MyCardSubclass
'supdateVisualElements
方法中。真正奇怪的是,当我记录这些标签的描述时,文本已更新,但不可见。这是它的样子:
在我的视图控制器的viewDidLoad
方法中,我初始化了一个UIScrollView
和我的卡片对象。我将其添加cardView
为子视图,如下所示:
-(void)viewDidLoad
{
[super viewDidLoad];
MyCardSubclass *myCard = [[MyCardSubclass alloc]initWithInfo:info andPoint:CGPointMake(5, runningYValue)];
[myCard setParentViewController:self];
[scrollView addSubview:[myCard cardView]];
//Resize scrollView contentSize
}
我的卡
//header
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *cardView;
@property (nonatomic, strong) CardInfo *cardInfo;
@property (nonatomic, strong) UIViewController *parentViewController;
//implementaion
-(UIView *)cardView
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIView *toReturn = [[UIView alloc]initWithFrame:CGRectMake(self.point.x,self.point.y, screenRect.size.width-borderOffset*2, cardViewHeight)];
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(borderOffset, 0, toReturn.frame.size.width-borderOffset*2, titleHeight)];
[titleLabel setText:self.title];
[toReturn addSubview:titleLabel];
[self.contentView setFrame:CGRectMake(borderOffset, titleHeight, screenRect.size.width-borderOffset, contentViewHeight)];
if(self.contentView)
[toReturn addSubview:self.contentView];
else
NSLog(@"no contentView");
//other view stuff
return toReturn;
}
我的卡子类
//header
@property(nonatomic, strong) UILabel *totalAmountLabel;
@property(nonatomic, strong) UILabel *availableAmountLabel;
@property(nonatomic, assign) double available;
//implementation
-(UIView *)cardView
{
[self setContentView:[self contentViewFromCardInfo:self.cardInfo]];
return [super cardView];
}
-(UIView *)contentViewFromCardInfo:(CardInfo *)cardInfo
{
UIView *toReturn = [[UIView alloc]init];
NSString *amount = @"0";
self.totalAmountLabel = [[UILabel alloc]initWithFrame:CGRectMake(labelWidth,
runningYValue,
labelWidth,
labelHeight)];
[self.totalAmountLabel setText:amount];
[toReturn addSubview:self.totalAmountLabel];
runningYValue +=self.totalAmountLabel.frame.size.height+spacer;
self.availableAmountLabel = [[UILabel alloc]initWithFrame:CGRectMake(labelWidth,
runningYValue,
labelWidth,
labelHeight)];
[self.availableAmountLabel setText:@"0"];
[toReturn addSubview:self.availableAmountLabel];
return toReturn;
}
-(void)updateVisualElements
{
NSString *amount = @"10"
NSString *availableString = @100"
//The log descriptions for these labels are okay.
[self.totalAmountLabel setText:amount];
NSLog(@"%@",self.totalAmountLabel.debugDescription);
[self.availableAmountLabel setText:availableString];
NSLog(@"%@",self.availableAmountLabel.debugDescription);
NSAssert([NSThread isMainThread], @"Not on main thread");
NSAssert(self.totalAmountLabel, @"No label");
}
我试过的
[self.totalAmountLabel setNeedsDisplay]
设置文本后调用使用在主线程上调用方法
[self.totalAmountLabel performSelector:@selector(setText:) withObject:amount afterDelay:0.0]