除了标签之外还有其他区分控件的方法吗? IBOutlet 怎么样?
如果我们使用 IBAction,我们就得到了发送者对象,对吗?如果我们还有一个指向该按钮的 IBOutlet,该出口是否会指向与发送者相同的地址?
那么我们如何测试平等呢?有人这样做吗?
If we use IBAction, we got the sender object right? If we also has an IBOutlet to that button, will that outlet point to the same address as sender?
How do we test for equality then? Anyone doing it this way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的 XIB 中有一个按钮,并将其连接到 IBOutlet,并将其操作连接到 IBAction,您将获得与 IBAction 中的 sender 参数相同的指针。因此,您可以简单地使用指针比较:
if (sender == myButton) { ... }
在某些情况下,使用标签是使用 IBOutlets/变量的替代方法(想想数字键盘的按钮) 。我认为它不像 IBOutlet 连接那么“明显”或容易看到,但我在开发数字键盘时使用过它:每个数字按钮都有一个与按钮编号相对应的标签值以及我必须做的所有事情正在评估标签。比十几次比较要好。
If you have a button in your XIB and connect that to your IBOutlet and its action to your IBAction, you will get the same pointer as sender argument in your IBAction. So you can simply use a pointer comparison:
if (sender == myButton) { ... }
Using a tag is an alternative to using IBOutlets/variables in some cases (think buttons for a number pad). I think it's not as "obvious" or easily visible as an IBOutlet connection but I've used it for example when developing a number pad: each number button had a tag value that corresponded to the number of the button and all I had to do was evaluate the tag. Better than a dozen comparisons.
当然,您没有理由不能对多个控件使用相同的操作并比较发送者。这几乎就是将发送者传递给操作的重点。
Sure, there's no reason you can't use the same action with multiple controls and compare the sender. That's pretty much the point of the sender being passed to actions.