0

I was going through a tutorial that seemed quite straightforward but could not get it to work. The goal is to have a label and a button in the UI view that are created with IB. The label is supposed to say "Hello World" and when you click the button it will change the text to "Hello iPhone". When I implement the code, the label doesn't change. when I debug, I found out the label is "null" but I can't figure out why... I would really appreciate your help with this.

The label has a tag value "55".

My code looks like this: in the interface file:

-(IBAction)buttonTapped;

in the main file:

-(IBAction)buttonTapped{
    UILabel *label = (UILabel*) [window viewWithTag:55];
    NSLog(@"The label's text is %s",label.text); //my debug statement

    if([label.text isEqualToString:@"Hello World"])
        label.text = @"Hello iPhone";
    else
        label.text = @"Hello World"; 
}
1
  • Is this label set up in your storyboard or created in code?
    – 8vius
    Jul 19, 2012 at 20:02

3 Answers 3

1

Assuming you're in a ViewController. I don't know what your window property is. Also you should be using "%@" for strings. Format Specifiers

-(IBAction) buttonTapped:(UIButton*)sender{    
    UILabel *label = (UILabel*) [self.view viewWithTag:55];
    NSLog(@"The label's text is %@",label.text); //my debug statement

    if([label.text isEqualToString:@"Hello World"])
        label.text = @"Hello iPhone";
    else
        label.text = @"Hello World";

}
0

Reading between the lines it sounds like your action method is not being fired.

You weren't explicit in saying how you knew the label text was blank, but can we assume that you did actually get the NSLog to output its results? If you didn't then I refer to my first sentence... Check that you have wired up your Button correctly in the Interface Builder, to point to an outlet action.

-1

in your @interface you should have:

IBOutlet UILabel *label;

then in your -(IBAction)buttonTapped just:

NSLog(@"The label's text is %s",label.text); //my debug statement

if([label.text isEqualToString:@"Hello World"])
        label.text = @"Hello iPhone";
else
        label.text = @"Hello World";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.