Quick Newbie Question

Apprentice
Posts: 5
Joined: 2010.04
Post: #1
Quick (hopefully) newbie question for anyone so gracious as to answer it Smile.


so I have this written as part of a little test thing to see if I know what I'm doing (apparently not), I have all the labels connected properly in interface builder.

If you push a button in the UI, method pushButton is called, which calls the method roll, if I do the strLabel.text = newText in the setLabel method, it does not set the label properly, if I do it directly in the pushButton method it works fine. NSLog is there in setLabel to test it and it outputs correctly in the console.

What am i doing wrong?

Code:
-(void)setLabel:(UILabel *) label : (int)roll
{
    NSString *newText = [[NSString alloc] initWithFormat:@"%d", roll];
    
    NSLog(@"test, %i %@", roll, newText);
    
    label.text = newText;
}

-(void)roll
{
    GameViewController *myController = [[GameViewController alloc] init];
    
    int stat = random() % 18;
    
    while (stat == 0)
    {
        stat = random() % 18;
    }
    
    [myController setLabel: strLabel :stat];    
}
Quote this message in a reply
Member
Posts: 27
Joined: 2008.12
Post: #2
I know you said that strLabel is connected properly, but that's the first thing I'd check.

Code:
NSLog(" strLabel is %@ ", strLabel);

If you see nil or null there, then strLabel is not connected.

Side notes: it's the typical style to have labels for all of the parameters in the method name. Also, you have a memory leak in setLabel since you init a string and never release it.

Code:
-(void)setLabel:(UILabel *) label withNumber: (int)roll
{
    NSString *newText = [[NSString alloc] initWithFormat:@"%d", roll];
    
    NSLog(@"test, %i %@", roll, newText);
    
    label.text = newText;

    [newText release];
}

-- Available Now: Dead Panic, a casual zombie shooter!
-- Development Blog: How to make a game under $1k
-- Twitter: xsmasher
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Newbie question. Mazinga 3 3,715 Apr 27, 2011 11:54 AM
Last Post: Mazinga
  Newbie question: simple Sesame Street-like app redbaron 2 2,846 Apr 25, 2010 12:42 PM
Last Post: redbaron
  Quick question on using arrays with CGFloats duncster 5 4,226 Dec 30, 2008 09:59 AM
Last Post: AnotherJake