Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

abcdefg12345

macrumors 6502
Original poster
Jul 10, 2013
281
86
I'm trying to figure out how to pass variables between classes correctly and i managed to import variables from other classes but i only edit them in the main class, anyone knows how i can edit the variables on other classes

heres what i did

Main Class.h

Code:
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>

@interface MySingletonCenter : NSObject {
        
@private
    double operand;
    NSString *StringToPass1;
    NSString *StringToPass2;
    NSString *StringToPass3;

}
@property double operand;
@property(readonly) NSString * StringToPass1;
@property(readonly) NSString * StringToPass2;
@property(readonly) NSString * StringToPass3;

@property (weak) IBOutlet NSTextField *Field1;
@property (weak) IBOutlet NSTextField *Field2;
@property (weak) IBOutlet NSTextField *Field3;

- (IBAction)Button:(id)sender;

@end

Main class.m
Code:
#import "MySingletonCenter.h"

@implementation MySingletonCenter

@synthesize operand;
@synthesize StringToPass1;
@synthesize StringToPass2;
@synthesize StringToPass3;

- (IBAction)Button:(id)sender{
    StringToPass1 = [NSString stringWithFormat:@"%@", _Field1.stringValue];
    StringToPass2 = [NSString stringWithFormat:@"%@", _Field2.stringValue];
    StringToPass3 = [NSString stringWithFormat:@"%@", _Field3.stringValue];
}

@end;

different class to import the string variable to

string.h

Code:
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import "MySingletonCenter.h"

@interface string : NSObject {
        
@private
    IBOutlet MySingletonCenter *brain;
}
@property (weak) IBOutlet NSTextField *Field1;
@property (weak) IBOutlet NSTextField *Field2;
@property (weak) IBOutlet NSTextField *Field3;

- (IBAction)Button:(id)sender;

@end

string.m

Code:
#import "string.h"
#import "MySingletonCenter.h"

@interface MySingletonCenter()
@property(readonly) MySingletonCenter *brain;
@end

@implementation string

- (MySingletonCenter *)brain {
    if (!brain) {
        brain = [[MySingletonCenter alloc] init];
    }
    return brain;
}

- (IBAction)Button:(id)sender{
    [_Field1 setStringValue:self.brain.StringToPass1];
    [_Field2 setStringValue:self.brain.StringToPass2];
    [_Field3 setStringValue:self.brain.StringToPass3];
}

@end
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.