UIButton 在 uitouch 事件上崩溃
我有一个 UIButton,它将 Draginside 事件发送到:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
它通过 viewDidLoad 中的以下代码执行此操作:
[colourButton1 addTarget:self action:@selector(touchesBegan:withEvent:) forControlEvents:UIControlEventTouchDown];
在将其发送到的方法中,有以下行:
UITouch *myTouch = [touches anyObject];
由于某种原因,当在 UIButton 内拖动时,这会使应用程序崩溃。有什么想法吗?
编辑:解决方案..
-(IBAction)buttonDragged:(id)button withEvent:(UIEvent*)event {
NSSet *touches = [event touchesForView:colourButton1];
[self touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event];
}
I have a UIButton which sends the draginside event to:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
It does this through the following code in viewDidLoad:
[colourButton1 addTarget:self action:@selector(touchesBegan:withEvent:) forControlEvents:UIControlEventTouchDown];
Within the method it sends it to, there is the following line:
UITouch *myTouch = [touches anyObject];
And for some reason, when dragging inside the UIButton this crashes the app. Any ideas why?
EDIT: The solution..
-(IBAction)buttonDragged:(id)button withEvent:(UIEvent*)event {
NSSet *touches = [event touchesForView:colourButton1];
[self touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您向控件添加目标时,您可以传递 3 种类型的操作选择器 -
名称并不重要,重要的是参数的数量。如果控件向其目标发送带有 2 个参数的消息,则第一个参数将是控件本身(在您的情况下是 UIButton 的实例),第二个参数是 UIEvent 的实例。但是您希望将
NSSet
实例作为第一个参数,并向其发送anyObject
消息,而UIButton
无法理解。这就是崩溃的原因。您为什么首先尝试将事件从 UI 控件发送到触摸处理方法
touchesMoved:withEvent:
?它可能会做一些与你的意思不同的事情。更新:
请注意,由于
touchesMoved:withEvent:
是UIResponder
的方法,而控制器的视图是UIResponder
此方法也会在此视图的触摸事件上调用。When you're adding target to control you can pass 3 types of selectors for action -
Names don't matter, it's number of parameter which is important. If control sends it's target a message with 2 params then first parameter will be control itself (instance of
UIButton
in your case), second one - instance ofUIEvent
. But you expects instance ofNSSet
as first parameter and send it a messageanyObject
whichUIButton
doesn't understand. This is the cause of crash.Why are you trying to send event from UI control to touch handling method
touchesMoved:withEvent:
in the first place? It will probably do something different from what you meant.UPDATE:
Notice that since
touchesMoved:withEvent:
is aUIResponder
's method and controller's view isUIResponder
this method will be also called on touch events for this view.