問題
UILabel
サブビューのいくつかのレイヤーに埋もれていて、テキストを変更できないようです。ラベルを更新するコードはMyCardSubclass
のupdateVisualElements
メソッドにあります。本当に奇妙なことは、これらのラベルの説明をログに記録すると、テキストが更新されているのに表示されないことです。外観は次のとおりです。
View Controller のviewDidLoad
メソッド内で、 aUIScrollView
と card オブジェクトを初期化します。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]