如何以编程方式更改在 nib 文件中创建的 UIView 中的标签?

发布于 2024-12-08 22:36:51 字数 976 浏览 1 评论 0原文

我无法以编程方式更改标签中的文本。

当我运行以下代码时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最偏执的依靠 2024-12-15 22:36:51

您在设置标签字符串的方法中使用了不正确的访问器名称。

在 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 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.

り繁华旳梦境 2024-12-15 22:36:51

确保代码中有一个用于标签的对象,如下例所示:

    IBOutlet UILabel* aLabel;  

在界面生成器中(您可能已经完成了此操作):将 aLabel(或您使用的任何名称)出口连接到实际标签。这可以通过控制单击并从文档窗口中的文件所有者对象拖动到视图中的标签来完成。将出现一个灰色的小窗口,其中至少有两个选项,一个是之前定义的 aLabel,另一个是视图(这是视图控制器所需的默认出口,它将有一个破折号表示它已经连接到某物)。单击 aLabel 选项将其选中。 (说实话,没有我的 Mac 在我面前,我复制了本段的大部分说明 此链接。)

您有一个名为 aLabel 的对象,您现在可以像对待任何其他变量一样对待它。如果您想更改文本值,请尝试以下操作:

aLable.text = @"some text";

也许您没有建立连接。你可以尝试以下方法吗?

#import "UIViewOverlay.h"

@implementation UIViewOverlay

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

@end

如果您无法打印原始值,则表明您尚未连接。

我希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文