user440096
user440096

Reputation:

UILabel nameLabel.text = "test1"; not working and the outlet is connected

I used the code to try set the label text but its not working. I though maybe I forgot to connect the outlet with the label, or i connected the wrong outlet but they were ok once I checked them. Made sure the xib was saved.

.m

@synthesize nameLabel;
@synthesize infoLabel;

-(void) updateUI
{
nameLabel.text = @"test1";
infoLabel.text = @"test2";
}

.h

UILabel * nameLabel;
UILabel * infoLabel;
@property(nonatomic, retain) IBOutlet UILabel *nameLabel;
@property(nonatomic, retain) IBOutlet UILabel *infoLabel;

Thats pretty much all the code used in the veiw controller related to these labels. Is there something i am missing that might explain this strangeness?

The default text i have in the labels 'name' & 'info' is what is showing.

This is the code that gets called prior to updateUI being called

browseDeckViewController.m

-(void) viewDidLoad 
{
    cardOnTopOfDeck = 0; 
    cardSecondFromTopOfDeck=1;
    deck = [[Deck alloc] init];
    [deck loadDeckData];
    Card *mySecondCard = [[Card alloc] init];
    mySecondCard = [deck.deckArray objectAtIndex:cardSecondFromTopOfDeck];
    secondCard = [[CardViewController alloc] initWithNibName:@"CardViewController"               
    bundle:[NSBundle mainBundle] numberOfStats:kNumStats];
    [secondCard setCard:mySecondCard];
    CGRect frame = secondCard.view.frame;
    frame.origin.x = (320-frame.size.width)/2;
    frame.origin.y = 10;
    secondCard.view.frame = frame;  
    [self.view addSubview:secondCard.view];


    topCard = [[CardViewController alloc] initWithNibName:@"CardViewController"    
    bundle:[NSBundle mainBundle] numberOfStats:kNumStats];
    Card *myTopCard = [[Card alloc] init];
    myTopCard = [deck.deckArray objectAtIndex:cardOnTopOfDeck];
    [topCard setCard:myTopCard];
    frame = topCard.view.frame;
    frame.origin.x = (320-frame.size.width)/2;
    frame.origin.y = 10;
    topCard.view.frame = frame;
    [self.view addSubview:topCard.view];
}

CardViewController.m

    -(void) setCard:(Card *)newCard 
    {
    [card release];
    card = [newCard retain];
    [self updateUI];
    }

   -(void) updateUI
   {
    NSLog(@"updateUI");
    nameLabel.text = @"test1";
    infoLabel.text = @"test2";
   }

Upvotes: 1

Views: 1216

Answers (2)

Ed Marty
Ed Marty

Reputation: 39700

Your CardViewController is created and then you immediately try to set the text of a UILabel in that controller's view. The problem is, the view isn't loaded yet. At the time updateUI is called, viewDidLoad: has not yet been called for the CardViewController, which means nameLabel and infoLabel are nil.

You have to force the CardViewController's view to load from the NIB before trying to access any of the outlets. a simple [self view]; would suffice. For example:

-(void) updateUI {
     NSLog(@"nameLabel's value: %@",nameLabel); //nil here
     [self view];
     NSLog(@"nameLabel's value after loading view: %@",nameLabel); //Now it's loaded
     nameLabel.text = @"test1";
     infoLabel.text = @"test2";
}

EDIT:

Another solution would be to move the call to updateUI to after addSubview:

myTopCard = [deck.deckArray objectAtIndex:cardOnTopOfDeck];
[self.view addSubview:topCard.view];
[topCard setCard:myTopCard];
frame = topCard.view.frame;
frame.origin.x = (320-frame.size.width)/2;
frame.origin.y = 10;
topCard.view.frame = frame;

Upvotes: 2

Shaggy Frog
Shaggy Frog

Reputation: 27601

This code is incorrect:

nameLabel.text = "test1";
infoLabel.text = "test2";

You are assigning char* pointers to an NSString* variable; you need to prefix the string with the @ symbol.

Assuming that the updateUI method is called, nameLabel and infoLabel both exist when it is called, this should work:

nameLabel.text = @"test1";
infoLabel.text = @"test2";

Upvotes: 1

Related Questions