问题设置UILabel的价值(iPhone SDK) [英] Problem setting Value for UILabel (iPhone SDK)

查看:76
本文介绍了问题设置UILabel的价值(iPhone SDK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iPhone SDK上相当新,所以如果这个答案显而易见,请原谅我。

I'm reasonably new at the iPhone SDK, so please forgive me if this answer's ridiculously obvious.

我有一个连接到.xib文件的ViewController(他们被称为FirstViewController)

I have a ViewController connected to a .xib file (they are called FirstViewController)

我还有一个名为MyCustomClass的自定义类。在这个课程中,我有一个这样的方法:

I also have a custom class called MyCustomClass. In this class, I have a method like this:

- (void)setTheText {
    [myLabel setText:@"foo"];
}

我想调用此方法在FirstViewController中设置标签中的文本。 xib

I want to call this method to set the text in a label in FirstViewController.xib

到目前为止,我已经这样做了:

So far, I have done this:

将对象拖到Interface Builder中,将类设置为:MyCustomClass
将IBOutlet'myLabel'从MyCustomClass连接到视图中的UILabel。

Dragged in an "Object" into Interface Builder, and set the class to: MyCustomClass Connected up the IBOutlet 'myLabel' from MyCustomClass to a UILabel in the view.

但是,当我运行程序时,按下按钮设置标签(在FirstViewController.m中),如:

However, when I run the program, and press the button to set the label (which is in FirstViewController.m), something like:

- (IBAction)doSomething:(id)sender {
   MyCustomClass *customClass = [MyCustomClass alloc] init];
   [customClass setTheText];
}

但这并没有设定。 NSLog(@%@,[myLabel text]); 返回(null)

This doesn't set though. NSLog(@"%@",[myLabel text]); returns (null)

Xcode显示没有错误或警告,可能出现什么问题?

Xcode shows no errors or warnings, what could be wrong?

谢谢,

Michael

其他信息:

MyCustomClass.h的接口:

Interface to MyCustomClass.h:

#import <UIKit/UIKit.h>

@interface MyCustomClass : NSObject {
    UILabel *myLabel;
}

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

- (void)setTheText;

@end


推荐答案

您的这里的customClass实例与NIB完全无关。您已使用+ alloc实例化了一个新对象。如果你想在你的NIB中修改特定的MyCustomClass,那么FirstViewController需要一个指向它的IBOutlet。

Your instance of customClass here is completely unrelated to the NIB. You've instantiated a new object using +alloc. If you want to modify the specific MyCustomClass in your NIB, then FirstViewController needs an IBOutlet that points to it.

这篇关于问题设置UILabel的价值(iPhone SDK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆