UIButton 在代码中设置触摸处理程序
我想要实现触摸一个UIButton
并让代码在与所有者不同的不同类中运行。
我意识到我可以对按钮的所有者(ClassA
)执行touchUpInside
,然后调用ClassB
内部的方法我想打电话,但是有什么办法可以加快吗?
想法:
让
的委托,ClassB
成为ClassA->UIButton
在编程中设置
touchUpInside
调用,以使用ClassB
内部的函数
我不知道如何实现这些想法:(输入是值得赞赏的!
I want to accomplish touching a UIButton
and having code run in a different class than the owner.
I realize I can do a touchUpInside
to the button's owner (ClassA
) and then call the method inside ClassB
that I want called, but is there any way to expedite this?
ideas:
have
ClassB
be the delegate for theClassA->UIButton
set the
touchUpInside
call in programming to used the function insideClassB
I'm not sure how to accomplish either of these ideas :( Input is mas appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种选择是使用设置按钮
,但这有点危险,因为
target
不会保留,因此您可以将消息发送到已释放的对象。您可以改为设置一个协议
然后在您的实现中
由于协议中定义的方法不是可选的,我可以改为检查
(self.delegate)
以确保它被设置而不是 <代码>respondsToSelector。One option is to set the button up using
but this is a bit dangerous because
target
is not retained so you could send the message to a deallocated object.You could instead set up a protocol
Then in your implementation
As the method defined in the protocol was not optional I could have instead done a check for
(self.delegate)
to make sure it is set instead ofrespondsToSelector
.