防止 UIAlertView 关闭

发布于 2024-08-15 06:31:29 字数 105 浏览 2 评论 0原文

作为一种验证形式,有什么方法可以防止按下“确定”按钮时警报视图消失?

场景:我在警报视图中有 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 技术交流群。

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

发布评论

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

评论(3

枫以 2024-08-22 06:31:29

iOS 5 向 UIAlertView 引入了一个新属性来解决这个问题。

alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

有关 UIAlertView 的 Apple 文档。

添加新的 UIAlertViewDelegate 方法来处理按钮的启用/禁用。

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView

有关 UIAlertViewDelegate 的 Apple 文档。

iOS 5 introduces a new property to UIAlertView to handle exactly this problem.

alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

Apple documentation on UIAlertView.

Add the new UIAlertViewDelegate method to handle the enabling/disabling of the button.

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView

Apple documentation on UIAlertViewDelegate.

掩饰不了的爱 2024-08-22 06:31:29

您这样做的方式是错误的,您应该根据输入启用和禁用提交按钮。首先,您必须访问该按钮。这很简单,只需创建不带按钮的警报,创建一个独立的按钮并将其添加到对话框中:

[alert addButtonWithTitle:@"OK"];
UIButton *submitButton = [[alert subviews] lastObject];
[submitButton setEnabled:…];

然后您必须为这些文本字段设置委托,并在字段更改时启用或禁用该按钮:

- (BOOL) textField: (UITextField*) textField
    shouldChangeCharactersInRange: (NSRange) range
    replacementString: (NSString*) string
{
    int textLength = [textField.text length];
    int replacementLength = [string length];
    BOOL hasCharacters = (replacementLength > 0) || (textLength > 1);
    [self setButtonsAreEnabled:hasCharacters];
}

// Disable the ‘Return’ key on keyboard.
- (BOOL) textFieldShouldReturn: (UITextField*) textField
{
    return NO;
}

当然,您应该包装所有内容将其放入一个单独的类中,这样您就不会弄乱您的调用代码。

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:

[alert addButtonWithTitle:@"OK"];
UIButton *submitButton = [[alert subviews] lastObject];
[submitButton setEnabled:…];

And then you have to set a delegate for those textfields and enable or disable the button when the fields change:

- (BOOL) textField: (UITextField*) textField
    shouldChangeCharactersInRange: (NSRange) range
    replacementString: (NSString*) string
{
    int textLength = [textField.text length];
    int replacementLength = [string length];
    BOOL hasCharacters = (replacementLength > 0) || (textLength > 1);
    [self setButtonsAreEnabled:hasCharacters];
}

// Disable the ‘Return’ key on keyboard.
- (BOOL) textFieldShouldReturn: (UITextField*) textField
{
    return NO;
}

Of course you should wrap all this into a separate class so that you don’t mess up your calling code.

紫轩蝶泪 2024-08-22 06:31:29

我不认为您实际上需要传递任何按钮名称。只需取出“确定”按钮字符串并将其保留为“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".

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