replace value of variable in another class

Go To StackoverFlow.com

0

I have classes *ColorViewController *DrawRectangle

In ColorViewController.h:

#import "DrawRectangle.h"
@interface ColorViewController : UIViewController {
DrawRectangle *DrawRectangle;
}

@property (nonatomic,retain) DrawRectangle *DrawRectangle;

And in DrawRectangle.h:

@interface DrawRectangle : UIView {
CGFloat redc;
}
@property (nonatomic) CGFloat redc;

I have also added the DrawRectangle class in ViewController.xib to one of UIView.

I want to change the value of CGFloat redc from file ColorViewController.m I'm doing it this way:

- (void)viewDidLoad
{
CGFloat myfloat = 1.0;
self.DrawRectangle.redc = myfloat;

I'm calling NSLog with this 'redc' in DrawRectangle, but it says 0.0000000 each time.

How can I replace the value of 'redc' from ViewController? I don't think I need to use NSNotification, there must be easier way if I have the DrawRectangle loaded into ViewController.

2012-04-05 23:02
by Konrad Kolasa
possible duplicate of Setting Bool in different classesChuck 2012-04-05 23:45


2

Looks like your object hasn't been instantiated yet. Try:

-(void)viewDidLoad {
    [super viewDidLoad];

    CGFloat myfloat = 1.0;
    [drawRectangle setRedc:myfloat];
}

You should also avoid naming your objects the same name as the class as a general programming practice. I Would suggest renaming the object to drawRectangle.

You also need to hook up your drawRectangle object in your xib

@property (nonatomic, retain) IBOutlet DrawRectangle *drawRectangle;
2012-04-05 23:06
by ColdLogic
[DrawRectangle setRedc:myfloat]; Why so? It doesn't seem to be working.. - Konrad Kolasa 2012-04-05 23:09
Why so what? Is your object synthesized? @synthesize redc; and @synthesize DrawRectangle;ColdLogic 2012-04-05 23:10
Yes, I have redc synthesized in DrawRectangle.m and DrawRectangle in ViewController of cours - Konrad Kolasa 2012-04-05 23:14
I have now `[self setDrawRectangle:[[DrawRectangle alloc] init]];

CGFloat myfloat = 1.0;
NSLog(@"%f", myfloat);

[drawRectangle setRedc:myfloat];` but doesn't work still
< - Konrad Kolasa 2012-04-05 23:14
if that is your code, your NSLog should output 1.000000000 because you are just outputting the CGFloat you just create - ColdLogic 2012-04-05 23:15
Yes, but I have two NSLog... the second one is located in DrawRectangle.m NSLog(@"Value %f",redc);Konrad Kolasa 2012-04-05 23:18
Where in DrawRectangle is it - ColdLogic 2012-04-05 23:19
let us continue this discussion in chatKonrad Kolasa 2012-04-05 23:20
Ads