iPhone SDK:通过代码修改使用界面生成器制作的标签
我有一个完整的 iPhone 项目。 我需要将其本地化。第一次使用 Mac,第一次使用 XCode 和 Objective C。我几乎迷失了方向。不管怎样,我正在尝试更改标签的值(我的第一个任务)。
当我进入“Interface builder”时,我有一个文件 .xib。我点击它,我看到带有一些“对象”(图像、标签等)的界面。然后我可以单击标签...如何更改文本值?我查看了相应的 .m 和 .h 文件,任何地方都没有 UILabel...
(我使用 xcode 4.2)
更新: 我可以通过双击 Interface Builder 中的对象来更改文本。现在我需要更改代码中的文本。
更新2 我能够使用 Jeroen 的解决方案来做到这一点。
这里有一些精度:
您需要添加 IBoutlet UILabel *myLabel; @interface 括号内的部分 YourViewController 部分
@property 在 .h 文件中变得松散
@synthesize 部分在 .m 文件中变得“松散”
[myLable 版本];部分位于 .m 文件的 (void)dealloc 方法内部
为了更改代码内的文本,我添加了以下代码:
self.myLabel.text = [NSString stringWithFormat:@"Test..."];
在 .m 文件的“viewDidLoad”方法内。
并进行拖放:
- 在“对象”框中(在界面生成器中)找到标签,
- 右键单击标签,
- 在“新引用插座”行的右侧 应该有一个黑色的小弹出窗口弹出窗口,有一个小圆圈。这就是你可以拖放的东西。将其拖放到“占位符”框中的“文件所有者”项上。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想更改 UILabel 的文本,则必须手动将其添加到 .h 和 .m 文件中,然后通过界面生成器将其耦合。
要耦合它,您应该在 .h 文件中添加一个 IBoutlet:例如
IBOutlet UILabel *myLabel;
并从中创建一个属性
@property(非原子,保留)IBOutlet UILabel *myLabel;
code>在 .m 文件中,您应该合成并释放分配。
例如。
@synthesize myLabel;
然后
,您将能够通过右键单击 UILabel 将 xib 中的 UILabel 连接到源文件,并将“新引用插座”连接到“文件所有者”中的正确标签,
但这不是 iOS 上的翻译应该如何进行的。他们在这里描述得非常好。 http://developer.apple.com/internationalization/
简而言之,Apple 建议为每种语言制作一个 xib 。您可以通过右键单击 xib,然后单击“使文件可本地化”来将其本地化。完成后,您可以单击名为“添加本地化”的按钮来添加新语言。
确保您的应用程序非常稳定,因为保持不同语言的最新状态很快就会变得令人厌烦。
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.
在 xcode 4.2 中,界面生成器确实可以帮助您。您需要做的是设置 Xcode,以便您可以看到故事板和控制器。
按住 ctrl 键的同时,单击标签并拖动。应该会出现一条线。将此行拖到视图控制器 .h 编辑器中并放开。应该会出现一个弹出窗口,要求您输入名称。您可以随意命名它,例如 uiLabel。
现在返回到您的视图控制器 .m 文件。如果其中没有名为 viewDidLoad 的方法,请创建一个:
每次视图即将出现在屏幕上时都会调用此方法。要将文本添加到标签,请实现如上所示的代码。
尝试此处看看它是如何完毕。转到“在 IB 对象和代码文件之间直接建立连接”
希望有帮助
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:
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