问题
Why isn't my UILabel being changed? I am using the following code, and nothing is happening:
- (void)awakeFromNib {
percentCorrect.adjustsFontSizeToFitWidth;
percentCorrect.numberOfLines = 3;
percentCorrect.minimumFontSize = 100;
}
Here is my Implemintation code:
- (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
Any ideas? Thanks
回答1:
In order for the
adjustsFontSizeToFitWidth
setting to come into effect, thenumberOfLines
property must be set to 1. It won't work if it's != 1.Are
awakeFromNib
,viewDidLoad
,viewWillAppear
being called at all?The
minimumFontSize
property will do nothing if the text fits in the current bounds with the current font. Did you set the font property for the label?percentCorrect.font = [UIFont systemFontOfSize:20];
Finally, isn't a
minimumFontSize = 100
a little too big for a min font size?
回答2:
Make sure everything is hooked up correctly. Make sure the IBOutlet for the UITextfield
is setup and set break points within the method and see that the code is being touched. If it is, it's possible percentCorrect
hasn't been hooked up correctly.
回答3:
You shouldn't have to init
your label if it is in the nib. If you are, then you created the label twice. So who knows which one you are messaging to. As soon as you initialized the label, you leaked the first one. So the label you have on screen is NOT the one you are manipulating in code.
Try placing your code in viewDidLoad
instead. It should be initialized by then.
If that doesn't work, try viewDidAppear:
simply to try to debug this.
回答4:
It's possible that percentCorrect
hasn't yet been initialized. Is percentCorrect
equal to nil
when that function is called, by any chance? If so, wait until after it's properly initialized to set its properties.
回答5:
What are you expecting to happen? Does the label show when your code is commented out? How is percentCorrect defined in the nib?
Have you tried:
- (void)awakeFromNib {
percentCorrect.adjustsFontSizeToFitWidth = YES;
percentCorrect.numberOfLines = 3;
percentCorrect.minimumFontSize = 100;
percentcorrent.text = @"What is the text in percentCorrect?";
}
回答6:
I had the same problem. Seems that setText doesn't automatically force a redraw when the change happens on a non-main thread. UI updates should always be done on the main thread to ensure responsiveness. There's another way to force it, using a selector:
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];
You may also get results from simply saying:
[label setNeedsDisplay];
which will force the update internally, but at the SDK's discretion. I found that didn't work for me, thus why I recommend the selector on the main thread.
回答7:
What I found is sometimes , don't rely too much on IB , just add a line to set the frame :
labelx.frame=CGRectMake(labelx.frame.origin.x,labelx.frame.origin.y, 300, labelx.frame.size.height);
Then , autoresize works !
来源:https://stackoverflow.com/questions/957918/why-isnt-my-uilabel-being-changed