iOS 工具栏有时会突出显示/选择错误的按钮
我有一个 iOS 应用程序,它在一个视图中用代码制作了一个工具栏。这里没什么特别的;只是一堆标准的 UIBarButtonItems(用 initWithImage 初始化,每个都调用不同的选择器),使用 UIToolbar setItems:animated(没有动画)添加(连同间隔符)到工具栏。
这在大多数情况下都工作得很好......但有时,在多次进入另一个视图并返回到这个视图后,工具栏的触摸处理似乎已关闭。它突出显示并调用错误的按钮。也不总是相同的错误按钮;有时它就在我实际点击的旁边,但有时它距离更远,甚至在工具栏的另一侧清晰可见。
它没有任何模式,但如果你尝试足够长的时间(切换到另一个视图,然后返回到这个视图),它总是会发生。当另一个视图处于不同的屏幕方向时(由其 shouldAutorotateToInterfaceOrientation 实现强制),这种情况似乎更常见,但即使所有视图具有相同的方向,它仍然会时不时地发生。当这种情况发生时,我的选择器将被调用,发送者与实际突出显示的按钮匹配,而不是我触摸的按钮。因此,这似乎确实是 iOS 中无法正确处理触摸的问题。
我的 google-fu 完全没有找到有关此问题的任何其他报告...看起来这一定是 Apple 的错误,因为我的代码都不涉及工具栏触摸处理。这种情况在 iOS 4.3 下的模拟器和设备上都会发生(尚未尝试其他版本)。
还有其他人遇到过这个问题吗?关于如何避免它有什么建议吗?
I have an iOS app that, in one view, makes a toolbar in code. Nothing fancy here; just a bunch of standard UIBarButtonItems (initialized with initWithImage, and each one invoking a different selector), added (along with spacers) to the toolbar using UIToolbar setItems:animated (with no animation).
This works fine most of the time... but sometimes, after going to another view and back to this one many times, the toolbar's touch-handling appears to be off. It highlights and invokes the wrong button. It's not always the same wrong button, either; sometimes it's one right next to the one I actually tapped, but other times it is further away, even clear over on the other side of the toolbar.
There is no pattern to it, but if you try long enough (switching to another view and then back to this one), it always happens. It seems to happen more often when the other view is at a different screen orientation (forced by its shouldAutorotateToInterfaceOrientation implementation), but even when all views have the same orientation, it still happens now and then. When it happens, my selector gets invoked with a sender that matches the button that was actually highlighted, rather than the one I touched. So it really seems to be a matter of something in iOS not processing the touch correctly.
My google-fu has utterly failed to turn up any other reports of this issue... seems like it must be an Apple bug, since none of my code is involved in the toolbar touch handling. This happens both in the simulator and on the device, under iOS 4.3 (haven't tried other versions yet).
Has anyone else run into this issue? Any suggestions on how to avoid it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我发现问题了。创建工具栏的不是代码本身;而是创建工具栏的代码。这是它被调用的地方(以及频率!)。
这是遗留代码,我们没有注意到创建工具栏代码是从视图的 didRotateFromInterfaceOrientation 方法调用的(出于我们无法想象的原因)。该方法被频繁调用,并且旧的工具栏没有被拆除,因此它在工具栏之上创建工具栏。在这种情况下,触摸处理变得不稳定也就不足为奇了。
删除那里的调用(并像人们期望的那样将其放入 viewDidLoad 中)解决了问题。很抱歉浪费了大家的时间……但也许下一个偶然发现奇怪的工具栏行为的人会发现这很有帮助。
OK, I found the problem. It wasn't the code that creates the toolbar itself; it was where (and how often!) it was being called.
This was legacy code, and we hadn't noticed that the create-the-toolbar code was being called from the view's didRotateFromInterfaceOrientation method (for reasons we can't imagine). That method gets invoked quite frequently, and the old toolbar wasn't being torn down, so it was creating toolbars on top of toolbars. Not too surprising that the touch handling went wonky in that situation.
Deleting the call there (and putting it in viewDidLoad like one would expect) solved the problem. Sorry for wasting everybody's time... but maybe the next person to stumble across screwy toolbar behavior will find this helpful.