6

为什么我的 UILabel 没有被更改?我正在使用以下代码,但没有任何反应:

- (void)awakeFromNib {
    percentCorrect.adjustsFontSizeToFitWidth;
    percentCorrect.numberOfLines = 3;
    percentCorrect.minimumFontSize = 100;
}

这是我的实现代码:

- (void) updateScore {
    double percentScore = 100.0 * varRight / (varWrong + varRight);
    percentCorrect.text = [NSString stringWithFormat:@"%.2f%%", percentScore];
}

- (void)viewDidLoad {
    percentCorrect.adjustsFontSizeToFitWidth = YES;
    percentCorrect.numberOfLines = 3;
    percentCorrect.minimumFontSize = 100;
    percentCorrect.text = @"sesd";
}


- (void)correctAns {
    numberRight.text = [NSString stringWithFormat:@"%i Correct", varRight];
}

-(void)wrongAns {
    numberWrong.text = [NSString stringWithFormat:@"%i Incorrect", varWrong];
}

#pragma mark Reset Methods
- (IBAction)reset:(id)sender; {
    NSString *message = @"Are you sure you would like to reset?";
    self.wouldYouLikeToReset = [[UIAlertView alloc] initWithTitle:@"Reset?" message:message delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
    [wouldYouLikeToReset addButtonWithTitle:@"Continue"];
    [self.wouldYouLikeToReset show];
    // Now goes to (void)alertView and see's what is being pressed!
}

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0)
    {
        NSLog(@"Cancel button pressed");
    }
    else
    {
        varRight = 0;
        varWrong = 0;
        [self wrongAns];
        [self correctAns];
        percentCorrect.text = [NSString stringWithFormat:@"0.0%%"];

    }
}

#pragma mark Button Action Methods

- (IBAction)right:(id)sender; {
    varRight++;
    [self correctAns];
    [self updateScore];
}

- (IBAction)wrong:(id)sender; {
    varWrong++;
    [self wrongAns];
    [self updateScore];
}


- (IBAction)subOneRight:(id)sender {
    if (varRight > 0 ) {
        varRight--;
        [self correctAns];
        [self updateScore];
    }
}


- (IBAction)subOneWrong:(id)sender {
    if (varWrong > 0) {
        varWrong--;
        [self wrongAns];
        [self updateScore];
    }
}

-(IBAction)addHalfCredit:(id)sender;
{
    varWrong++;
    varRight++;
    [self wrongAns];
    [self correctAns];
    [self updateScore];
}


@end

有任何想法吗?谢谢

4

7 回答 7

20
  1. 为了使adjustsFontSizeToFitWidth设置生效,该numberOfLines属性必须设置为 1。如果它是 != 1,它将不起作用。

  2. awakeFromNib, viewDidLoad,是否viewWillAppear被调用?

  3. minimumFontSize如果文本符合当前字体的当前边界,则该属性将不执行任何操作。您是否为标签设置了字体属性?

    percentCorrect.font = [UIFont systemFontOfSize:20];

  4. 最后,minimumFontSize = 100对于最小字体大小来说是不是有点太大了?

于 2010-07-22T21:56:39.830 回答
2

确保一切都正确连接。确保设置了 IBOutletUITextfield并在方法中设置断点,并查看代码是否被触及。如果是,则可能percentCorrect没有正确连接。

于 2009-06-05T20:19:12.790 回答
2

init如果它在笔尖中,你不应该有你的标签。如果是,那么您创建了两次标签。所以谁知道你在给哪一个发消息。一旦你初始化了标签,你就泄露了第一个。所以你在屏幕上的标签不是你在代码中操作的标签。

尝试将您的代码放入viewDidLoad其中。那时它应该被初始化。

如果这不起作用,请尝试viewDidAppear:简单地尝试调试它。

于 2009-06-06T01:05:01.073 回答
0

你期待发生什么?当您的代码被注释掉时,标签会显示吗?笔尖中的percentCorrect是如何定义的?

你有没有尝试过:

- (void)awakeFromNib {
    percentCorrect.adjustsFontSizeToFitWidth = YES;
    percentCorrect.numberOfLines = 3;
    percentCorrect.minimumFontSize = 100;
    percentcorrent.text = @"What is the text in percentCorrect?";
}
于 2009-06-06T10:57:51.780 回答
0

可能percentCorrect尚未初始化。是否percentCorrect等于nil调用该函数的时间?如果是这样,请等到它正确初始化后再设置其属性。

于 2009-06-05T20:15:41.343 回答
0

我有同样的问题。当更改发生在非主线程上时,似乎 setText 不会自动强制重绘。UI 更新应始终在主线程上完成,以确保响应能力。还有另一种强制它的方法,使用选择器:

label = [[UILabel alloc] init];   //assumes label is a data member of some class
...
(some later method where you want to update the label)
...
[label performSelectorOnMainThread:@selector(setText) withObject:@"New label value" waitUntilDone:false];

您也可以通过简单地说:

[label setNeedsDisplay]; 

这将在内部强制更新,但由 SDK 自行决定。我发现这对我不起作用,因此我推荐主线程上的选择器。

于 2010-04-06T16:43:48.683 回答
0

我发现有时,不要太依赖 IB,只需添加一行来设置框架:

labelx.frame=CGRectMake(labelx.frame.origin.x,labelx.frame.origin.y, 300, labelx.frame.size.height);

然后,自动调整大小的作品!

于 2013-04-07T10:02:46.253 回答