Maciej Chrzastek
Maciej Chrzastek

Reputation: 1490

Passing float value from ViewController to some Class

I'm new to objective-c programming and I stuck. I have uislider in my viewcontroller and float variable in header file of viewcontroller. I also have a new class which changes look of my second view, I overwritten drawRect.

I simply take value of slider.value and save it in my float variable then I try to access float variable in my class because this variable defines width of lines which i want to draw.

header file of my view controller

@interface ViewController : UIViewController 
{
float lineWidth;
}

@property float lineWidth;
-(float)changingline;
@end

viecontroller.m //sliderForLine = UISlider

- (IBAction)valuechangedForLine:(UISlider *)sender 
{

float sliderValue = sliderForLine.value;
lineWidth = sliderValue;
[self changingline];

}
-(float)changingline
{   
NSLog(@"%f", lineWidth);

return lineWidth;
} 

NSLog says there is some value in lineWidth

class which specifies how the second UIView should looks like

#import "ViewController.h"

ViewController *oldView = [[ViewController alloc] init];
CGContextSetLineWidth(ctx, oldView.lineWidth);
NSLog(@"%f",oldView.lineWidth);

and here, in second class, NSLog says always 0.

I'm confused, I don't even know now why I'm using -(void)changeling :)

All I know is that I'm missing the main point how to pass values between classes, can somebody explain me what I'm doing wrong?

Thanks in advance :]

Upvotes: 0

Views: 1700

Answers (2)

Draco
Draco

Reputation: 1003

In your question, you describe a class ViewController that subclasses UIViewController. You show the interface (.h) and you show two methods from the implementation (.m). You show code where a new object of the class (oldView) is allocated and initialized. However, you do not show how the a new class object is initialized (you do not show its init method).

lineWidth is a property of the the object oldView, and you refer to it immediately after you initialize oldView in these lines:

ViewController *oldView = [[ViewController alloc] init]; 
CGContextSetLineWidth(ctx, oldView.lineWidth); 
NSLog(@"%f",oldView.lineWidth); 

Yet, nowhere do you show where the value of lineWidth is initialized. Maybe you didn't initialize it. Maybe - (IBAction)valuechangedForLine:(UISlider *)sender didn't get called. Could that be your problem?

Upvotes: 0

CodaFi
CodaFi

Reputation: 43330

You NEED (not want) a delegate for this. Try something like this:

header file of my view controller

@protocol ViewControllerDelegate <NSObject>

@required

-(void)valueChanged:(float)value;

@end

@interface ViewController : UIViewController 
{
float lineWidth;
id<ViewControllerDelegate> _delegate;
}

@property float lineWidth;
@property (nonatomic, assign) id<ViewControllerDelegate> delegate;
-(float)changingline;
@end

//.m

@synthesize delegate = _delegate;
...

- (IBAction)valuechangedForLine:(UISlider *)sender 
{

float sliderValue = sliderForLine.value;
lineWidth = sliderValue;
[self.delegate valueChanged:lineWidth];

}

Then all you need to do is conform to the protocol with a pair of these <> and implement the required method.

Also, rename your iVar with an underscore (so, instead of lineWidth, use _lineWidth) then @synthesize lineWidth = _lineWidth

Upvotes: 1

Related Questions