Reputation: 43
Hey I need help with an if statement that seems to not be working.
Basically I have a UILabel that displays a result for a racing series. Now if that round has not been run yet, the results should not be in there and not be displaying the UILabel.
I put a value of 'NA' in the label (not applicable) and ran an if statement on it to say if the results of this data equals to NA then don't bother making the label. But it makes it anyway and prints out the data 'NA' on it.
Here is my code:
- (void)viewDidLoad {
// Create the scroll view for this view
scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.contentSize = CGSizeMake(320, 220);
[scrollView setFrame:CGRectMake(0, 180, 320, 188)];
// Create all the non-changing labels
UILabel *date_title = [[UILabel alloc] initWithFrame:scrollView.bounds];
date_title.textColor = [UIColor whiteColor];
date_title.text = @"Event date:";
date_title.font = [UIFont boldSystemFontOfSize:12];
[date_title setBackgroundColor:[UIColor clearColor]];
[date_title setFrame:CGRectMake(10, 35, 100, 12)];
[scrollView addSubview:date_title];
[date_title release];
UILabel *direction_title = [[UILabel alloc] initWithFrame:scrollView.bounds];
direction_title.textColor = [UIColor whiteColor];
direction_title.text = @"Direction:";
direction_title.font = [UIFont boldSystemFontOfSize:12];
[direction_title setBackgroundColor:[UIColor clearColor]];
[direction_title setFrame:CGRectMake(10, 55, 100, 12)];
[scrollView addSubview:direction_title];
[direction_title release];
UILabel *tracklength_title = [[UILabel alloc] initWithFrame:scrollView.bounds];
tracklength_title.textColor = [UIColor whiteColor];
tracklength_title.text = @"Track Length:";
tracklength_title.font = [UIFont boldSystemFontOfSize:12];
[tracklength_title setBackgroundColor:[UIColor clearColor]];
[tracklength_title setFrame:CGRectMake(10, 75, 100, 12)];
[scrollView addSubview:tracklength_title];
[tracklength_title release];
UILabel *winner2010_title = [[UILabel alloc] initWithFrame:scrollView.bounds];
winner2010_title.textColor = [UIColor whiteColor];
winner2010_title.text = @"2010 Winners";
winner2010_title.font = [UIFont boldSystemFontOfSize:12];
[winner2010_title setBackgroundColor:[UIColor clearColor]];
[winner2010_title setFrame:CGRectMake(10, 105, 300, 12)];
[scrollView addSubview:winner2010_title];
[winner2010_title release];
winner2011_title = [[UILabel alloc] initWithFrame:scrollView.bounds];
winner2011_title.textColor = [UIColor whiteColor];
winner2011_title.font = [UIFont boldSystemFontOfSize:12];
[winner2011_title setBackgroundColor:[UIColor clearColor]];
[winner2011_title setFrame:CGRectMake(10, 160, 300, 12)];
[scrollView addSubview:winner2011_title];
[winner2011_title release];
// Do the labels for the round_name
name = [[UILabel alloc] initWithFrame:scrollView.bounds];
name.textColor = [UIColor whiteColor];
name.font = [UIFont boldSystemFontOfSize:16];
[name setBackgroundColor:[UIColor clearColor]];
[name setFrame:CGRectMake(10, 10, 280, 16)];
[scrollView addSubview:name];
[name release];
// Do the labels for the round_date
dates = [[UILabel alloc] initWithFrame:scrollView.bounds];
dates.textColor = [UIColor whiteColor];
dates.font = [UIFont systemFontOfSize:12];
[dates setBackgroundColor:[UIColor clearColor]];
[dates setFrame:CGRectMake(110, 35, 200, 12)];
[scrollView addSubview:dates];
[dates release];
// Do the labels for the round_direction
direction = [[UILabel alloc] initWithFrame:scrollView.bounds];
direction.textColor = [UIColor whiteColor];
direction.font = [UIFont systemFontOfSize:12];
[direction setBackgroundColor:[UIColor clearColor]];
[direction setFrame:CGRectMake(110, 55, 200, 12)];
[scrollView addSubview:direction];
[direction release];
// Do the labels for the round_track_length
track_length = [[UILabel alloc] initWithFrame:scrollView.bounds];
track_length.textColor = [UIColor whiteColor];
track_length.font = [UIFont systemFontOfSize:12];
[track_length setBackgroundColor:[UIColor clearColor]];
[track_length setFrame:CGRectMake(110, 75, 200, 12)];
[scrollView addSubview:track_length];
[track_length release];
// Do the labels for the 2010 winners
winner2010 = [[UILabel alloc] initWithFrame:scrollView.bounds];
winner2010.textColor = [UIColor whiteColor];
winner2010.font = [UIFont systemFontOfSize:12];
winner2010.numberOfLines = 0;
[winner2010 setBackgroundColor:[UIColor clearColor]];
[winner2010 setFrame:CGRectMake(10, 120, 300, 30)];
[scrollView addSubview:winner2010];
[winner2010 release];
// Do the labels for the 2011 winners
winner2011 = [[UILabel alloc] initWithFrame:scrollView.bounds];
winner2011.textColor = [UIColor whiteColor];
winner2011.font = [UIFont systemFontOfSize:12];
winner2011.numberOfLines = 0;
[winner2011 setBackgroundColor:[UIColor clearColor]];
[winner2011 setFrame:CGRectMake(10, 175, 300, 30)];
[scrollView addSubview:winner2011];
[winner2011 release];
// Add the content to the main scrollview
[self.view addSubview:scrollView];
[scrollView release];
}
- (void)viewDidAppear:(BOOL)animated {
if(![round_2011_winner isEqualToString:@"NA"]){
winner2011_title.text = @"2011 Winners";
winner2011.text = round_2011_winner;
}
name.text = round_name;
dates.text = round_date;
direction.text = round_direction;
track_length.text = round_track_length;
winner2010.text = round_2010_winner;
}
Now I have set up an alert box to show what the value is when the view loads, and it value is correct, if it has run, it shows the value of who won, and when it has not run, it shows the value NA.
Could this be because the UILabel is set to numberofline = 2? Something to do with a character return or something?
The label is within a scrollview as well.
Upvotes: 0
Views: 2976
Reputation: 458
Try using
if (![round_2011_winner isEqualToString:@"NA"]) {
...
}
Upvotes: 1