views:

32

answers:

2

I am trying to send a slider value from a controller object to a method of a model object. The later is implemented in the separate file and I have appropriate headers. I think the problem is that I am not sure how to instantiate the receiver in order to produce a working method for the controller.

Here is the controller's method.

-(IBAction)setValue:(id)slider {[Model setValue:[slider floatValue]];}

@implementation Model
-(void)setValue:(float)n{
    printf("%f",n);
}
@end

What I get is 'Model' may not respond to '+setValue' warning and no output in my console.

Any insight is appreciated.

+1  A: 

you should allocate the modal first because the method is an instance method and cannot be used as an class(static) method.

Use Model *modelObject = [[Model alloc] init]; [modelObject setValue:2];

Rahul Vyas
I don't think you can include something like Model *modelObject = [[Model alloc] init]; among the instance variable declarations? Correct me if I am wrong. Also my argument is a slider value, not a plain 2. So I am looking for a way for the Model class to instantiate slider value.
seaworthy
i have just put a sample value you can put slider.value instead of 2.
Rahul Vyas
A: 

Another way to do this, if this makes sense for your project, is to 1) in your nib file add an object controller 2) set the class of that object controller to your model class 3) in your model class create an instance variable for the slider: float sliderFloatValue 4) create accessors for the float :@property (readwrite, assign) float sliderFloatValue; @synthesize sliderFloatValue; 5) bind the slider value to the sliderFloatValue of your model class

regulus6633
If I understand you correctly what you are suggesting to do is this?@interface Model{ float sliderFloatValue;}@end@implementation Model-(void)setValue:(float)n{ printf("%f",n);}@end@interface Controller:NSObject{ Model *variable;}@end@implementation Controller-(IBAction)setValue:(id)slider {[variable setValue:[slider floatValue]];}@end
seaworthy