views:

1047

answers:

6

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

A: 

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.

Adam Rosenfield
you're right, I forgot to init. But, still wont work :/
Daniel Kindler
+2  A: 

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.

Benny Wong
Everything is hooped up, because the label is responding to my IBActions
Daniel Kindler
Can you change anything on that label programmatically in other methods? The IBActions can still be called without the IBOutlet being hooked up, so make sure the label is definitely hooked up as the IBOutlet
Benny Wong
oh. Well its hooked up in IB
Daniel Kindler
+1  A: 

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.

Corey Floyd
That didnt work :/
Daniel Kindler
A: 

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?";
}
Roger Nolan
Nope. Still not working :/I added my code into the question, so maybe there is a problem there.
Daniel Kindler
A: 

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.

Daver
Messages to UIKit classes and instances thereof, and calls to UIKit functions, should always be done on the main thread **period**. See the recently-updated docs: http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CocoaFundamentals/AddingBehaviortoaCocoaProgram/AddingBehaviorCocoa.html#//apple_ref/doc/uid/TP40002974-CH5-SW40
Peter Hosey
A: 
  1. In order for adjustsFontSizeToFitWidth 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?

ettore