问题描述
例如,我有一个 IBAction ,它在界面生成器中连接到 UIButton 。
- (IBAction)functionToBeCalled:(id)sender
{
// do something here
}
在我的代码中,是调用 IBAction 的最佳方法?
如果我尝试像这样调用,我会收到一个错误:
[self functionToBeCalled:];
但是,如果我试图像这样调用罚款:
[self functionToBeCalled:0];
正确调用它的正确方法是什么?
正确的方式是:
- [self functionToBeCalled:nil ]
传递nil发件人,表示它没有通过通常的框架调用。 p>
或
- [self functionToBeCalled: self]
将自己作为发件人,这也是正确的。
要选择哪一个取决于函数究竟是什么,以及它期望发送者是什么。
Say for instance I have an IBAction that is hooked up to a UIButton in interface builder.
- (IBAction)functionToBeCalled:(id)sender
{
// do something here
}
With-in my code, say for instance in another method, what is the best way to call that IBAction?
If I try to call it like this, I receive an error:
[self functionToBeCalled:];
But, if I try to call it like this (cheating a bit, I think), it works fine:
[self functionToBeCalled:0];
What is the proper way to call it properly?
The proper way is either:
- [self functionToBeCalled:nil]
To pass a nil sender, indicating that it wasn't called through the usual framework.
OR
- [self functionToBeCalled:self]
To pass yourself as the sender, which is also correct.
Which one to chose depends on what exactly the function does, and what it expects the sender to be.
这篇关于从代码中调用IBAction的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!