0

问题

我有一个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]

4

2 回答 2

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];
于 2012-11-28T04:13:53.213 回答
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;
}

感谢您的意见和评论。

于 2012-11-29T01:00:08.973 回答