我有UILabel
隐藏在几层子视图中,我似乎无法更改它的文本。更新标签的代码采用MyCardSubclass
的{{1}}方法。真的很奇怪的是当我记录这些标签的描述时,文本已经更新,但它不可见。这就是它的样子:
在我的View Controller的updateVisualElements
方法中,我初始化了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]
答案 0 :(得分:0)
您可以尝试一件事,使用 Tag
属性来获取添加的标签并更改其竞争
示例:强>
UILabel totalAmountLabel = [[UILabel alloc]initWithFrame:CGRectMake(labelWidth,
runningYValue,
labelWidth,
[totalAmountLabel setText:amount];
totalAmountLabel.tag = 100;
[toReturn addSubview:totalAmountLabel];
然后在 updateVisualElements()
UILabel *tempTotalAmountLabel = (UILabel *)[self.view viewWithTag:100];
[tempTotalAmountLabel setText:amount];
答案 1 :(得分:0)
修正了它!问题实际上并非我试图设置标签或绘制其视图。
我没有正确使用我的初始化程序来初始化我的内容视图。
我在子类中删除了这个覆盖
-(UIView *)cardView
{
[self setContentView:[self contentViewFromCardInfo:self.cardInfo]];
return [super cardView];
}
我还从我的超类中的-(UIView *)cardView
方法中获取了代码,并将其放入-(UIView *)buildCardView
方法并将cardView属性访问器重写为原来的内容。
-(UIView *)cardView
{
if(!_cardView)
_cardView = [self buildCardView];
return _cardView;
}
感谢您的意见和评论。