在 iOS 5 上以编程方式关闭 UIAlertView 不会调用 didDismiss 委托方法

发布于 2024-12-09 17:31:37 字数 186 浏览 0 评论 0原文

我遇到了一个问题,当我调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

征﹌骨岁月お 2024-12-16 17:31:37

为了确保 iOS4 和 5 之间的行为一致,您可以在调用其 dismissWithClickedButtonIndex:animated: 方法之前删除 UIAlertView 的委托,然后手动调用该委托方法。例如

- (void)somethingDidHappen {
    id<UIAlertViewDelegate> delegate = myAlertView.delegate;
    myAlertView.delegate = nil;
    // now, we know the delegate won't be called...
    [myAlertView dismissWithClickedButtonIndex:0 animated:NO];
    // ...so we call it ourselves below
    [delegate alertView:myAlertView clickedButtonAtIndex:0];
}

(该代码尚未经过测试,但您明白了。)

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.

- (void)somethingDidHappen {
    id<UIAlertViewDelegate> delegate = myAlertView.delegate;
    myAlertView.delegate = nil;
    // now, we know the delegate won't be called...
    [myAlertView dismissWithClickedButtonIndex:0 animated:NO];
    // ...so we call it ourselves below
    [delegate alertView:myAlertView clickedButtonAtIndex:0];
}

(That code isn't tested, but you get the point.)

给不了的爱 2024-12-16 17:31:37

仅当用户执行操作时才会调用 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

恏ㄋ傷疤忘ㄋ疼 2024-12-16 17:31:37

根据 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.

乄_柒ぐ汐 2024-12-16 17:31:37

alertView:clickedButtonAtIndex:alertView:didDismissWithButtonIndex:alertView:willDismissWithButtonIndex:。仅当用户明确点击警报视图上的按钮(因此“单击”)时,才会调用您所引用的方法 (clickedButtonAtIndex:)。

通过 dismissWithClickedButtonIndex:animated: 来消除警报的编程调用似乎没有调用 alertView:clickedButtonAtIndex:

因此,如果您需要在关闭警报视图时始终触发某些行为(无论是由用户点击按钮触发还是以编程方式触发),那么请使用 didDismissWithButtonIndex:willDismissWithButtonIndex: 更有意义。

There are alertView:clickedButtonAtIndex:, alertView:didDismissWithButtonIndex: and alertView: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 call alertView: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: and willDismissWithButtonIndex: makes more sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文