Why isn't my UILabel being changed?

♀尐吖头ヾ 提交于 2019-12-23 09:40:14

问题


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:


  1. In order for the adjustsFontSizeToFitWidth setting to come into effect, the numberOfLines property must be set to 1. It won't work if it's != 1.

  2. Are awakeFromNib, viewDidLoad, viewWillAppear being called at all?

  3. 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];

  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!