开发者

What's an easy way to set up object communication in Obj-C?

开发者 https://www.devze.com 2022-12-27 05:49 出处:网络
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 a

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 s开发者_运维知识库ure 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.


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];


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

0

精彩评论

暂无评论...
验证码 换一张
取 消