How to programmatically change label in UIView created in a nib file?

2.3k views Asked by At

I have trouble changing the text in a label programmatically.

When I run the following code, NSLog does display "Setting myLabel to = Hello World!", but the label on the screen is not changed.

    UIViewOverlay *overlayWindow;
    overlayWindow = [[[NSBundle mainBundle] loadNibNamed:@"UIViewOverlay" owner:self options:nil] objectAtIndex:0];
    [self addSubview:overlayWindow];

    [overlayWindow setMyLabel:@"Hello World!"];

My NIB file has a 300x300 window with some labels and buttons. There is a label, which is connected to myLabel in the outlet. The UIView does display, just that the text cannot be changed programmatically.

UIViewOverlay.h

#import <UIKit/UIKit.h>

@interface UIViewOverlay : UIView {
    IBOutlet UILabel *myLabel;
}
- (void)setMyLabel:(NSString *) label;

@end

UIViewOverlay.m

#import "UIViewOverlay.h"

@implementation UIViewOverlay

- (void)setMyLabel:(NSString *) label {
    myLabel.text = label;                // THIS LINE IS NOT WORKING! :-(
    NSLog(@"Setting myLabel to = %@", label);    // This line is working.
 }

@end

Thanks in advance..

2

There are 2 answers

2
jrturton On BEST ANSWER

You are using an incorrect accessor name for your method to set the label string.

In cocoa, setFoo is the method by which an instance variable called foo is assigned. This isn't just a convention, many areas of functionality depend on it, for example the use of properties, key value coding etc.

In your code, your label is called myLabel. Your method to set the text of that label is called setMyLabel. Either this is causing your outlet to not be connected when the nib is loaded, as the runtime may be trying to use that method to assign the label to your instance variable in the first place, or all of the above has no effect and you have just not connected your outlet.

0
Jim On

Make sure you have an object in your code for the label, like this example:

    IBOutlet UILabel* aLabel;  

And in interface builder (you may have already done this): Connect the aLabel (or whatever name you use) outlet to the actual label. This can be done by control clicking and dragging from the File’s Owner object, in the document window, to the label, in the view. A small, gray window will appear with at least two options, one will be the aLabel defined earlier, and the other will be the view (this is a default outlet required for viewcontrollers, it will have a dash to indicate it is already connected to something). Click on the aLabel option to select it. (I'll be honest, without my mac in front of me I copied most of this paragraph's instructions from this link.)

You have an object called aLabel that you can now treat like any other variable. If you want to change the text value, try this:

aLable.text = @"some text";

Maybe you did not make the connection. Can you try the following?

#import "UIViewOverlay.h"

@implementation UIViewOverlay

- (void)showMyLabel:(NSString *) label {
    NSLog(@"My current label contents (myLabel) = %@", myLabel.text);    // ** changed to myLabel.text
 }

@end

If you aren't able to print the original value, then you aren't connected.

I hope that helps.