从代码中调用IBAction的最好方法是什么? [英] What's the best way to call an IBAction from with-in the code?

查看:520
本文介绍了从代码中调用IBAction的最好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个 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的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆