Why doesn't dismissWithClickedButtonIndex ever call clickedButtonAtIndex?

╄→尐↘猪︶ㄣ 提交于 2019-12-18 19:02:57

问题


http://iphonedevelopment.blogspot.com/2009/02/alert-view-with-prompt.html

I'm using that code to get my iPhone app to display an alertView with an included UITextField and matching CANCEL and OK buttons.

The user can enter text, hit CANCEL or OK... and I see the text the user entered... using my code inside the clickedButtonAtIndex method.

It all works except for 1 thing: Instead of OK, the user might hit DONE on the keyboard. I'm using dismissWithClickedButtonIndex to simulate an OK click... but then clickedButtonAtIndex never gets called.

Shouldn't dismissWithClickedButtonIndex also call clickedButtonAtIndex?

I'm trying to get clickedButtonAtIndex to get called if CANCEL, or OK, or even DONE is hit.


回答1:


Since this is an old question you've probably figured it out by now, but I was incredibly frustrated with this and couldn't find any help from Google, so for anyone who comes across this in the future: when you call dismissWithClickedbuttonIndex, the method that gets called is alertView:didDismissWithButtonIndex, not alertView:clickedButtonAtIndex. It behaves exactly the same way, and I can't figure out why they'd be two separate methods for what seems to be the same action, but there it is.




回答2:


clickedButtonAtIndex is called when user actually clicks the button. Clicking the button may or may not dismiss the alert. the standard OK/Cancel buttons have the effect of dismissing the alert on click. If a click translates to dismiss or a dismissWithClickedbuttonIndex method is called, then did DismissWithButtonIndex is called.




回答3:


I'm not sure whether I fully understand your question, but here is my try:

Firstly, you need to remove the if (buttonIndex != [alertView cancelButtonIndex]) from the example. This should work for the OK and CANCEL buttons.

To recognize the DONE key of the keyboard, use - (BOOL)textFieldShouldReturn:(UITextField *)textField of the UITextFieldDelegate. Here you could call [textField resignFirstResponder] to dismiss the keyboard.

Also, what to you do in clickedButtonAtIndex? Couldn't you create your own method and the call it when you need it?




回答4:


I had the same problem with UIAlertView(Blocks) categoty UIAlertView-Blocks

My solution:UIAlertView(Blocks) -> MYAlertView : UIAlertView

Sometimes method:- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex called. Sometimes it didn't call.

So... I tried to find solution in many sources. Then I'he created subclass of UIAlertView with absolutely same code. And my problem was solved. In fact it is little bit more difficult to use. But no more this intrusive bugs.



来源:https://stackoverflow.com/questions/4472854/why-doesnt-dismisswithclickedbuttonindex-ever-call-clickedbuttonatindex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!