How can I simply change a class variable from another class in ObjectiveC?
Posted
by Daniel
on Stack Overflow
See other posts from Stack Overflow
or by Daniel
Published on 2010-04-07T15:04:04Z
Indexed on
2010/04/11
10:33 UTC
Read the original article
Hit count: 268
I simply want to change a variable of an object from another class. I can compile without a problem, but my variable always is set to 'null'. I used the following code:
Object.h:
@interface Object : NSObject {
//...
NSString *color;
//...
}
@property(nonatomic, retain) NSString* color;
+ (id)Object;
- (void)setColor:(NSString*)col;
- (NSString*)getColor;
@end
Object.m:
+(id)Object{
return [[[Object alloc] init] autorelease];
}
- (void)setColor:(NSString*)col {
self.color = col;
}
- (NSString*)getColor {
return self.color;
}
MyViewController.h
#import "Object.h"
@interface ClassesTestViewController : UIViewController {
Object *myObject;
UILabel *label1;
}
@property UILabel *label1;
@property (assign) Object *myObject;
@end
MyViewController.m:
#import "Object.h"
@implementation MyViewController
@synthesize myObject;
- (void)viewDidLoad {
[myObject setColor:@"red"];
NSLog(@"Color = %@", [myObject getColor]);
[super viewDidLoad];
}
The NSLog message is always Color = (null)
I tried many different ways to solve this problem, but no success. Any help would be appreciated.
© Stack Overflow or respective owner