在 iOS 5 上以编程方式关闭 UIAlertView 不会调用 didDismiss 委托方法
我遇到了一个问题,当我调用 UIAlertView 的 dismissWithClickedButtonIndex:animated: 时,十有八九,委托方法alertView:willDismissWithButtonIndex: 不会被调用。还有其他人遇到这个问题吗?我即将向 Apple 报告错误,但我很好奇是否有其他人遇到过这个问题并找到了解决方法。
I'm running into a problem where 9 times out of ten, when I call UIAlertView's dismissWithClickedButtonIndex:animated:, the delegate method alertView:willDismissWithButtonIndex: is not called. Is anyone else running into this problem? I'm about to file a bug with Apple but I'm curious to see if anyone else has run into this issue and figured out any workarounds.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了确保 iOS4 和 5 之间的行为一致,您可以在调用其 dismissWithClickedButtonIndex:animated: 方法之前删除 UIAlertView 的委托,然后手动调用该委托方法。例如
(该代码尚未经过测试,但您明白了。)
To ensure a consistent behavior across iOS4 and 5, you could just remove the UIAlertView's delegate just prior to calling its dismissWithClickedButtonIndex:animated: method, then manually invoke the delegate method. e.g.
(That code isn't tested, but you get the point.)
仅当用户执行操作时才会调用 UI 对象的委托。 Apple 假设当您通过代码执行某些操作时,您已经知道自己在做什么,并且不需要被告知。这适用于所有委托(UIScrollView 的滚动委托方法与代码滚动、表视图操作等)
无论如何,应该使用哪个按钮索引来调用委托?..当您以编程方式解雇时没有人
Delegates of UI objects are only called when the user performs an action. Apple assumes that when you do something from code, you already know what you're doing and you don't need to be informed. That applies to all delegates (scrolling delegate methods of UIScrollView vs. code-scrolling, Table View manipulation, ...)
Anyway, what button index should the delegate be called with?.. there is no one when you dismiss programmatically
根据 Why does notmissWithClickedButtonIndex never call clickedButtonAtIndex? 问题是正在调用不同的方法。但是,这并不能解释为什么您会接到不稳定的电话。在我测试的设备上,解雇方法被正确调用,因此我仅将其重定向到点击版本。
如果您继续看到不稳定的行为,也许您应该向 Apple 提交错误。
According to Why doesn't dismissWithClickedButtonIndex ever call clickedButtonAtIndex? the problem is that a different method is being called. However, that doesn't explain why you get erratic calls. On the devices I tested the dismiss method gets called correctly, so I only redirect it to the click version.
Maybe you should file a bug with Apple if you continue seeing the erratic behaviour.
有
alertView:clickedButtonAtIndex:
、alertView:didDismissWithButtonIndex:
和alertView:willDismissWithButtonIndex:
。仅当用户明确点击警报视图上的按钮(因此“单击”)时,才会调用您所引用的方法 (clickedButtonAtIndex:
)。通过
dismissWithClickedButtonIndex:animated:
来消除警报的编程调用似乎没有调用alertView:clickedButtonAtIndex:
。因此,如果您需要在关闭警报视图时始终触发某些行为(无论是由用户点击按钮触发还是以编程方式触发),那么请使用
didDismissWithButtonIndex:
和willDismissWithButtonIndex:
更有意义。There are
alertView:clickedButtonAtIndex:
,alertView:didDismissWithButtonIndex:
andalertView:willDismissWithButtonIndex:
. The method that you're referring to (clickedButtonAtIndex:
) is only called when the user explicitly taps on a button on your alert view (hence 'clicked').Programmatic calls via
dismissWithClickedButtonIndex:animated:
to dismiss the alert does not seem to callalertView:clickedButtonAtIndex:
.So, if you need some behavior to be always triggered upon the dismissal of the alert view—whether it was triggered by the user tapping on a button or triggered programmatically—then using the
didDismissWithButtonIndex:
andwillDismissWithButtonIndex:
makes more sense.