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 :
- Find the label inside the “Objects” box (in the Interface Builder)
- Right click on the Label, there should be a little black popup
- 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.
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
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.