我对 iPhone SDK 相当陌生,所以如果这个答案非常明显,请原谅我。
我有一个连接到 .xib 文件的 ViewController(它们被称为 FirstViewController)
我还有一个名为 MyCustomClass 的自定义类。在这个类中,我有一个这样的方法:
- (void)setTheText {
[myLabel setText:@"foo"];
}
我想调用此方法在 FirstViewController.xib 的标签中设置文本
到目前为止,我已经这样做了:
将“对象”拖入 Interface Builder,并将类设置为: MyCustomClass 将 IBOutlet 'myLabel' 从 MyCustomClass 连接到视图中的 UILabel。
但是,当我运行程序并按下按钮设置标签(位于 FirstViewController.m 中)时,类似于:
- (IBAction)doSomething:(id)sender {
MyCustomClass *customClass = [MyCustomClass alloc] init];
[customClass setTheText];
}
虽然这并没有设置。NSLog(@"%@",[myLabel text]);
返回(空)
Xcode 没有显示错误或警告,可能是什么问题?
谢谢,
迈克尔
附加信息:
MyCustomClass.h 的接口:
#import <UIKit/UIKit.h>
@interface MyCustomClass : NSObject {
UILabel *myLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *myLabel;
- (void)setTheText;
@end