我正在尝试学习如何使用一个主要数据类为不同的类设置变量。
这是我想做的事情的图表以及我的项目中的代码:
A类
#import <Foundation/Foundation.h>
@interface ClassA : NSObject {
NSString *stringA;
NSString *stringB;
}
@property (nonatomic, copy) NSString *stringA;
@property (nonatomic, copy) NSString *stringB;
@property (weak) IBOutlet NSTextField *textA;
@property (weak) IBOutlet NSTextField *textB;
- (IBAction)displayStrings:(id)sender;
@end
#import "ClassA.h"
@implementation ClassA
@synthesize stringA, stringB, textA, textB;
- (IBAction)displayStrings:(id)sender {
[textA setStringValue:stringA];
[textB setStringValue:stringB];
}
@end
X 级
#import <Foundation/Foundation.h>
@interface ClassX : NSObject {
NSMutableString *stringX;
}
- (void)theVariables:(id)sender;
@end
#import "ClassX.h"
#import "ClassA.h"
@implementation ClassX
- (void)awakeFromNib {
[self theVariables:self];
}
- (void)theVariables:(id)sender {
stringX = [[NSMutableString alloc] init];
ClassA *clssA = [[ClassA alloc] init];
[stringX setString:@"stringX for stringA"];
[clssA setStringA:stringX];
[stringX setString:@"stringX for stringB"];
[clssA setStringB:stringX];
}
@end
代码中没有显示错误,但是当我运行程序时,我收到一个关于“无效参数不满足:aString”的错误。看起来 IBOutlet 的 setStringValue 不起作用。有什么建议么?