在 Objective c 的方法中使用变量
有没有办法在方法中定义一个对象,调用另一个对象的方法,并在该方法中使用第一个对象:
-(IBAction)method{
class * instance = [[class alloc] initWithValue:value];
[instance method];
}
class.m 文件中定义的方法:
-(void) method {
instance.value = someOtherValue;
}
Is there any way define a object in a method, call a method of another object, and in that method use the first object:
-(IBAction)method{
class * instance = [[class alloc] initWithValue:value];
[instance method];
}
The method defined in class.m file:
-(void) method {
instance.value = someOtherValue;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的解决方案是将其作为参数传递:
但是,为了避免将两个类耦合得太紧密,通常使用协议来定义回调的语义,并将方法调用与回调的规范分开。回调处理程序首先分配委托,然后调用方法。这有点复杂,我希望我已经正确地涵盖了所有细节。
The simple solution is to pass it in as a parameter:
To avoid coupling the two classes together too tightly, however, it is common to use a protocol to define the semantics of the callback, and to separate the method-call from the specification of the callback handler by assigning a delegate first, then calling methods. It's a bit involved, and I hope I have correctly covered all the details.