如何以编程方式更改在 nib 文件中创建的 UIView 中的标签?
我无法以编程方式更改标签中的文本。
当我运行以下代码时,NSLog 确实显示“Setting myLabel to = Hello World!”,但屏幕上的标签没有更改。
UIViewOverlay *overlayWindow;
overlayWindow = [[[NSBundle mainBundle] loadNibNamed:@"UIViewOverlay" owner:self options:nil] objectAtIndex:0];
[self addSubview:overlayWindow];
[overlayWindow setMyLabel:@"Hello World!"];
我的 NIB 文件有一个 300x300 的窗口,其中有一些标签和按钮。 有一个标签,它连接到插座中的 myLabel。 UIView 确实显示,只是文本不能以编程方式更改。
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
提前致谢..
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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在设置标签字符串的方法中使用了不正确的访问器名称。
在 cocoa 中,
setFoo
是分配名为foo
的实例变量的方法。这不仅仅是一个约定,许多功能领域都依赖于它,例如属性的使用、键值编码等。在您的代码中,您的标签称为
myLabel
。设置该标签的文本的方法称为setMyLabel
。这要么会导致您的插座在加载笔尖时无法连接,因为运行时可能会尝试使用该方法首先将标签分配给您的实例变量,要么以上所有内容都没有效果,您有只是没有连接你的插座。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 calledfoo
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 calledsetMyLabel
. 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.确保代码中有一个用于标签的对象,如下例所示:
在界面生成器中(您可能已经完成了此操作):将 aLabel(或您使用的任何名称)出口连接到实际标签。这可以通过控制单击并从文档窗口中的文件所有者对象拖动到视图中的标签来完成。将出现一个灰色的小窗口,其中至少有两个选项,一个是之前定义的 aLabel,另一个是视图(这是视图控制器所需的默认出口,它将有一个破折号表示它已经连接到某物)。单击 aLabel 选项将其选中。 (说实话,没有我的 Mac 在我面前,我复制了本段的大部分说明 此链接。)
您有一个名为 aLabel 的对象,您现在可以像对待任何其他变量一样对待它。如果您想更改文本值,请尝试以下操作:
也许您没有建立连接。你可以尝试以下方法吗?
如果您无法打印原始值,则表明您尚未连接。
我希望这有帮助。
Make sure you have an object in your code for the label, like this example:
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:
Maybe you did not make the connection. Can you try the following?
If you aren't able to print the original value, then you aren't connected.
I hope that helps.