在 Obj-C 中设置对象通信的简单方法是什么?
我正在尝试将滑块值从控制器对象发送到模型对象的方法。后者在单独的文件中实现,并且我有适当的标头。我认为问题是我不确定如何实例化接收器以便为控制器生成工作方法。
这是控制器的方法。
-(IBAction)setValue:(id)slider {[Model setValue:[slider floatValue]];}
@implementation Model
-(void)setValue:(float)n{
printf("%f",n);
}
@end
我得到的是“Model”可能不会响应“+setValue”警告,并且我的中没有输出安慰。
任何见解都值得赞赏。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该首先分配模式,因为该方法是实例方法,不能用作类(静态)方法。
使用 Model *modelObject = [[Model alloc] init];
[模型对象设置值:2];
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];
另一种方法是,如果这对您的项目有意义,则 1) 在您的 nib 文件中添加一个对象控制器 2) 将该对象控制器的类设置为您的模型类 3) 在您的模型类中创建一个实例变量滑块:float sliderFloatValue 4)为float创建访问器:@property (readwrite, allocate) float sliderFloatValue; @synthesize sliderFloatValue; 5) 将滑块值绑定到模型类的 sliderFloatValue
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