在Cocoa中的不同类中设置变量 [英] Setting variables in a different class in Cocoa

查看:141
本文介绍了在Cocoa中的不同类中设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图学习如何使用一个主数据类为不同的类设置变量。

I'm trying to learn how to set variables for different classes using one main data class.

这是我想要做的和我的项目的代码的图:

Here's a diagram of of what I would like to do and the code from my project:

ClassA

#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不工作。任何建议?

No errors show up in the code, but when I run the program I am receiving an error about "Invalid parameter not satisfying: aString". It looks like the setStringValue for the IBOutlet is not working. Any suggestions?

推荐答案

我没有看到您提及的错误,但就我可以从您的代码,主要问题是这行:

I'm not seeing the error you mention, but as far as I can tell from your code, the main problem is this line:

ClassA *clssA = [[ClassA alloc] init];

您必须有 ClassA 你的xib,它连接到文本字段和一个按钮。 xib中的对象是一个真实的对象,如果你只是在代码中的某处创建 ClassA 的另一个实例,你有一个完全不同的对象,

You must have an instance of ClassA in your xib, which is connected to text fields and a button. That object in the xib is a real object, and if you just create another instance of ClassA somewhere in your code, you have an entirely different object that has no connection to the one that's in your xib.

你需要确保/改变两件事情。首先,在你的xib中需要有一个 ClassX 的实例。第二, ClassX 需要一个出口到 ClassA 实例:

You need to make sure of/change two things. First, there needs to be an instance of ClassX in your xib. Second, ClassX needs an outlet to a ClassA instance:

@class ClassA;    // Declare ClassA so you can use it below

@interface ClassX : NSObject

@property (weak) IBOutlet ClassA * theClassAInstance;

- (void)theVariables:(id)sender;

@end

然后在xib文件中连接。然后,在 theVariables:中,您只需使用该插件,而不是创建 ClassA 的新实例: [[self theClassAInstance] setStringA:@stringX for stringA];

Which should then be connected in the xib file. Then, in theVariables:, you just use that outlet instead of creating a new instance of ClassA: [[self theClassAInstance] setStringA:@"stringX for stringA"];

三点风格:

首先,您应该导入Cocoa.h: #import< Cocoa / Cocoa.h> 而不是Foundation.h在任何触及GUI的类中( ClassA 在这种情况下)。这就是定义 NSTextField 的地方。它工作无论如何,因为Cocoa.h通过您的.pch文件导入,但最好是显式。

First, you should be importing Cocoa.h: #import <Cocoa/Cocoa.h> instead of Foundation.h in any class that touches the GUI (ClassA in this case). That's where stuff like NSTextField is defined. It works anyways because Cocoa.h is imported via your .pch file, but it's best to be explicit.

其次,创建一个可变字符串并将其值更改为两个不同文字字符串不会有很大的意义。只需直接使用文字: [clssA setStringA:@stringX for stringA];

Second, creating a mutable string and changing its value to two different literal strings doesn't make a whole lot of sense. Just use the literals directly: [clssA setStringA:@"stringX for stringA"];

不需要单独声明实例变量; @synthesize 为您创建,现在建议您不要声明:

Third, You don't need to declare the instance variables separately; @synthesize creates them for you, and it is now the recommended practice to not declare them:

@interface ClassA : NSObject

@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

最后(四分!在 ClassA 中访问 stringA stringB 的值属性: [textA setStringValue:[self stringA]];

Last (four points!), you should really be accessing the values of stringA and stringB in ClassA via the property: [textA setStringValue:[self stringA]];

这篇关于在Cocoa中的不同类中设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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