mrmuggles
mrmuggles

Reputation: 2141

IPhone SDK : Modifying a label made with the interface builder by code

I have a full done IPhone project. I need to localize it. First time on a mac, first time with XCode and objective C. I'm pretty much lost out there. Anyway, I'm trying to change the value of a label (my first task).

When I go into "Interface builder", I have a file .xib. I click on it, I see the interface with some "objects" (images, labels, etc.). Then I can click on a label... How can I change the text value? I've looked into the corresponding .m and .h file, there's no UILabel anywhere...

(I use xcode 4.2)

UPDATE: I was able to change the text by double clicking the object in Interface Builder. Now I need to change the text in the code.

UPDATE 2 I was able to do it using Jeroen's solution.

Here are some precisions:

You need to add the IBoutlet UILabel *myLabel; part inside the brackets of the @interface YourViewController part

The @property goes loose inside the .h file

The @synthesize part goes "loose" inside the .m file

The [myLable release]; part goes inside the method (void)dealloc of the .m file

To change the text inside the code, I added this code :

self.myLabel.text = [NSString stringWithFormat:@"Test..."];

inside the "viewDidLoad" method of the .m file.

And to do the drag'n'drop :

  1. Find the label inside the "Objects" box (in the Interface Builder)
  2. Right click on the Label, there should be a little black popup
  3. At the right of the line "New Referencing Outlet" inside the popup, there is a little circle. That's the thing that you can drag'n'drop. Drag and drop this on the "File's Owner" item in the "Placeholders" box.

Upvotes: 1

Views: 2135

Answers (2)

GWed
GWed

Reputation: 15653

In xcode 4.2 interface builder really helps you out. What you need to do is setup Xcode so that you can see your storyboard and your controller.

While holding down the ctrl key, click on the label and drag. A line should appear. Drag this line over to your view controller .h editor and let go. A pop up should come up that asks you for a name. Call it whatever your want, like uiLabel.

Now go back to your view controller .m file. If there isn't a method in there called viewDidLoad, create one:

-(void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.uiLabel.text = @"hello world!";
}

This method is called every time your view is about to appear on screen. To add text to your label, implement the code as shown above.

Try here to see how its done. Go down to 'Make Connections Directly Between IB Objects and Your Code Files'

Hope thats helps

Upvotes: 1

Jeroen Coupé
Jeroen Coupé

Reputation: 1404

If you want to change the text of an UILabel you have to add it manually to the .h en the .m files and then couple it via the interface builder.

To couple it you should add an IBoutlet in your .h file: eg IBOutlet UILabel *myLabel;

and make a property from it @property (nonatomic, retain) IBOutlet UILabel *myLabel;

In the .m file you should synthesize and dealloc.

eg. @synthesize myLabel; and

[myLabel release]; 

Afterwards you will be able to connect the UILabel from the xib to the sourcefile by right clicking it and connect 'New Referencing Outlet' to the correct label in the 'File's Owner'

However that is not how translations on iOS are supposed to be doen. They describe it very nicely here. http://developer.apple.com/internationalization/

In short Apple recommends to make a xib per language. You can make a xib localizable by right clicking on it and then clicking Make 'File Localizable'. After you've done that you can click on a button that is called 'Add Localization' to add a new language.

Make sure that your app is pretty stable, because keeping up to date with different language is becoming tiresome pretty fast.

Upvotes: 1

Related Questions