防止 UIAlertView 关闭
作为一种验证形式,有什么方法可以防止按下“确定”按钮时警报视图消失?
场景:我在警报视图中有 2 个用于用户名/密码的文本字段。如果两者都是空并且用户按“确定”,我不希望解除警报。
As a form of validation, is there any way to prevent an alert view from dismissing when pressing an "OK" button?
Scenario: I have 2 text fields in the alertview for username/password. If both are empty and the user presses "OK", I do not want the alert to be dismissed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
iOS 5 向
UIAlertView
引入了一个新属性来解决这个问题。有关 UIAlertView 的 Apple 文档。
添加新的
UIAlertViewDelegate
方法来处理按钮的启用/禁用。有关 UIAlertViewDelegate 的 Apple 文档。
iOS 5 introduces a new property to
UIAlertView
to handle exactly this problem.Apple documentation on UIAlertView.
Add the new
UIAlertViewDelegate
method to handle the enabling/disabling of the button.Apple documentation on UIAlertViewDelegate.
您这样做的方式是错误的,您应该根据输入启用和禁用提交按钮。首先,您必须访问该按钮。这很简单,只需创建不带按钮的警报,创建一个独立的按钮并将其添加到对话框中:
然后您必须为这些文本字段设置委托,并在字段更改时启用或禁用该按钮:
当然,您应该包装所有内容将其放入一个单独的类中,这样您就不会弄乱您的调用代码。
You’re doing it the wrong way, you should enable and disable the submit button according to the input. First you have to get access to the button. This is easy, just create the alert without buttons, create a standalone button and add it to the dialog:
And then you have to set a delegate for those textfields and enable or disable the button when the fields change:
Of course you should wrap all this into a separate class so that you don’t mess up your calling code.
我不认为您实际上需要传递任何按钮名称。只需取出“确定”按钮字符串并将其保留为“nil”即可。
I don't believe you actually need to pass in any button names. Just take out your OK button string and leave it as "nil".