Objective C 中的声明放在哪里

发布于 2024-09-25 15:02:17 字数 148 浏览 5 评论 0原文

我有一个 file1.h file1.m file1.xib,xib 有一个空白屏幕,上面有一个标签。我有另一个文件,它执行一些任意计算,并将在计算循环时更新标签。我应该在 file1.h 或执行计算的文件中的哪里声明 UILabel * 值?

谢谢你开车到处走走。

I have a file1.h file1.m file1.xib the xib has a blank screen with a label on it. I have another file that does some arbitrary calculation and will update the label as the calculation loops. Where should I declare the UILabel * value in file1.h or the file that does the calculations?

Thank you drive around.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

弥繁 2024-10-02 15:02:17

该标签应声明为 IBOutlet,正如 Josh 在您的 .h 文件中所说的那样,并且是在 Interface Builder 中连接您的标签。

您还可以在 .h 文件中将标签定义为 @property 并将其合成到 .m 文件中,以便您可以使用 轻松访问“myLabel”。操作员。

现在,要使用计算更新标签,只需在 .h 文件中定义 updateLabel 函数并编写代码以在实现文件中实现更新,如下所示:


@interface File1 {
    IBOutlet UILabel *myLabel;
}

@property (nonatomic, retain) IBOutlet UILabel *myLabel;

- (void)updateLabel:(id)sender;

@end


@implementation File1
@synthesize myLabel;

 - (id)init {
     if ( (self = [super init]) ) {
          // init custom work here
     }
     return self;
 }

 - (void)updateLabel:(id)sender {

 //Here sender can be any button who call this function or send it nil if not

      //update your label here 
      myLabel.text = @"Updated Text";
      ......
 }

@end

The label should be declared as an IBOutlet as Josh said in your .h file as above and yes connect your label in Interface Builder.

You can also define your label as @property in .h file and synthesize it in .m file so that you can easily access "myLabel" using . operator.

Now to update your label with your calculations, simply define the updateLabel function in .h file and write your code to implement update in the implementation file as below:


@interface File1 {
    IBOutlet UILabel *myLabel;
}

@property (nonatomic, retain) IBOutlet UILabel *myLabel;

- (void)updateLabel:(id)sender;

@end


@implementation File1
@synthesize myLabel;

 - (id)init {
     if ( (self = [super init]) ) {
          // init custom work here
     }
     return self;
 }

 - (void)updateLabel:(id)sender {

 //Here sender can be any button who call this function or send it nil if not

      //update your label here 
      myLabel.text = @"Updated Text";
      ......
 }

@end

北方的韩爷 2024-10-02 15:02:17

该标签应在 .h 文件中声明为 IBOutlet

@interface File1 {
    IBOutlet UILabel *mylabel;
}

@end

确保将此插座连接到 Interface Builder 中的标签。

The label should be declared as an IBOutlet in your .h file:

@interface File1 {
    IBOutlet UILabel *mylabel;
}

@end

Make sure you connect this outlet to your label in Interface Builder.

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