Setting variables in a different class in Cocoa

痞子三分冷 提交于 2020-02-08 04:17:52

问题


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

Class 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

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?


回答1:


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];

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.

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

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"];


Three points of style:

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.

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"];

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

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



来源:https://stackoverflow.com/questions/10356822/setting-variables-in-a-different-class-in-cocoa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!