如何在iPhone中禁用Alertview的按钮?

问题描述 投票:7回答:5

我有具有2个按钮“确定”和“取消”和文本字段的警报视图。现在我要禁用“确定”按钮,直到用户在文本字段中输入一些文本。我怎样才能做到这一点?在此先感谢

iphone uialertview
5个回答
1
投票

您可以为“确定”和“取消”创建两个按钮。然后将这两个按钮添加为UIAlertView中的子视图。现在,通过检查文本字段中的文本(文本长度),您可以执行启用和禁用操作。


37
投票

更新2:对于Swift 5.1

<#your alert controller#>.addTextField {(tf) in

                        //... set your tf characteristics i.e .keyboardType here

                        NotificationCenter.default.addObserver(forName: UITextField.textDidChangeNotification,
                                                               object: tf,
                                                               queue: OperationQueue.main) { _ in

                                                                //enable or disable the selected action depending on whether the textField text is empty
                                                                <#your alert controller#>.actions[0].isEnabled = !tf.text!.isEmpty

                        }

                    }

发布此内容以更新自ios 5起的响应:

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
  UITextField *textField = [alertView textFieldAtIndex:0];
  if ([textField.text length] == 0)
  {
    return NO;
  }
  return YES;
}

UPDATE:iOS 8自从Apple弃用UIAlertView以来,都推荐使用UIAlertController。不再有[alertViewShouldEnableFirstOtherButton:]的委托调用

因此,您可以通过UITextFieldTextDidChangeNotification设置按钮启用属性使用

将textView添加到警报中
  • (void)addTextFieldWithConfigurationHandler:(void(^)(UITextField * textField))configurationHandler
[<#your alert#> addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.delegate = self;
textField.tag = 0; //set a tag to 0 though better to use a #define
}];

然后实现委托方法

  • (void)textFieldDidBeginEditing:(UITextField *)textField
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//in here we want to listen for the "UITextFieldTextDidChangeNotification"

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(textFieldHasText:)
                                         name:UITextFieldTextDidChangeNotification
                                       object:textField];

}

当textField中的文本更改时,将调用“ textFieldHasText:”并传递NSNotification *

-(void)textFieldHasText:(NSNotification*)notification{
//inside the notification is the object property which is the textField
//we cast the object to a UITextField*
if([[(UITextField*)notification.object text] length] == 0){
//The UIAlertController has actions which are its buttons.
//You can get all the actions "buttons" from the `actions` array
//we have just one so its at index 0

[<#your alert#>.actions[0] setEnabled:NO];
}
else{

[<#your alert#>.actions[0] setEnabled:YES];
}
}

完成后不要忘记删除观察者


4
投票

我想通过添加此内容来扩展Ryan Forsyth的答案。如果添加默认样式的UIAlertView,则尝试访问不存在的文本字段时可能会超出范围,因此请首先检查视图样式。


0
投票

[不知道您的应用程序的上下文,以下内容可能不适用-但是您阅读了iOS Human Interface Guidelines吗?听起来如果您经常会向用户显示UIAlertView的替代方案,似乎更好。


0
投票

与您的问题并没有真正的联系,但是如果您不想拒绝您的应用,请不要修改默认的UIAlertView。如果我没看错,您是在将文本字段添加到Alertview中,不是吗?就像登录视图一样。您应该创建自己的视图。

© www.soinside.com 2019 - 2024. All rights reserved.