如何以编程方式判断 IBAction 是否已被代码或用户操作调用
如何以编程方式判断 IBAction 是由代码还是由用户操作调用。
例如 我有一个方法,-(IBAction)someMethodWithIts:(id)sender
我已将其链接到 UISegmentedControl 上的 valueChanged 。
它可以通过以下方式调用:
- 用户更改段
- 在调用代码中设置 selectedIndex
[self someMethodWithIts:foo];
有没有办法区分调用是否来自第一种方式?
干杯
萨姆
How can I tell programmatically if a IBAction has been called by code or by user action.
Eg
I have a method, -(IBAction)someMethodWithIts:(id)sender
which I have linked to a valueChanged on a UISegmentedControl.
It can be called by,
- User changing segment
- setting the selectedIndex in code
- calling
[self someMethodWithIts:foo];
Is there a way to distinguish if the call has come from the first way?
Cheers
Sam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您可以将
nil
作为发送者(这是传统的)传递并使用它来指示它是以编程方式发送的,那就可以了。但我认为其他任何东西都太脆弱了,你应该像这样分解代码:如果你真的想要一个发送者,那么你可以这样做:
但一般来说,如果你希望 IBAction 与编程更改不同,那么不要将程序更改连接到 IBAction。
If you can pass
nil
as the sender (which is traditional) and use that to indicate it was sent programmatically, that's ok. But anything else I believe is too fragile and you should break up the code like this:If you really want a sender, then you can do it this way:
But in general, if you want the IBAction to be different than programatic changes, then don't wire programatic changes to the IBAction.
可能有用。或者查看 UIControl 或 UISegmentedControl 上定义的发送者的其他属性。也许
状态
。我的猜测是,您可以发现用户交互时和不交互时的不同之处。Might work. Or poke around other properties of sender defined on UIControl or UISegmentedControl. Maybe
state
. My guess is you can find something that's different when the user is interacting vs. when they're not.你确实想使用这样的方法
如果你发现
event
参数是nil
,那么它来自代码中的调用,否则,调用来自用户事件。You really want to use method like this
If you find the
event
parameter isnil
, it's from a call in your code, otherwise, the call is from user event.