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

时间:2011-02-24 07:18:13

标签: iphone uialertview

我有2个按钮的警报视图" OK"和"取消"和文本字段。 现在我想禁用" OK"按钮,直到用户在文本字段中输入一些文本。 我怎样才能做到这一点? 提前谢谢

5 个答案:

答案 0 :(得分:37)

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

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

更新: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];
}
}

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

答案 1 :(得分:4)

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

-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView*)alertView
{
    if(alertView.alertViewStyle == UIAlertViewStyleLoginAndPasswordInput ||
       alertView.alertViewStyle == UIAlertViewStylePlainTextInput ||
       alertView.alertViewStyle == UIAlertViewStyleSecureTextInput)
    {
        NSString* text = [[alertView textFieldAtIndex:0] text];
        return ([text length] > 0);
    }
    else if (alertView.alertViewStyle == UIAlertViewStyleDefault)
        return true;
    else
        return false;
}

答案 2 :(得分:1)

您可以为Ok和Cancel创建两个按钮。然后通过检查文本字段中的文本(文本长度)将这两个按钮添加为UIAlertView.Now中的子视图,您可以执行启用和禁用操作。

答案 3 :(得分:0)

在不知道您的申请背景的情况下,以下内容可能不适用 - 但您是否阅读过iOS Human Interface Guidelines?听起来好像你可能会更好地找到UIAlertView的替代品,如果这是经常会向用户显示的东西。

答案 4 :(得分:0)

与您的问题没有关系,但如果您不希望拒绝您的应用,请不要修改默认的UIAlertView。如果我没错,你会在短信中添加文本字段,不是吗?就像登录视图一样。您应该创建自己的视图。

因此,关于您的问题,创建视图,设置按钮已禁用,并委派UITextFields。什么时候

- (void)textFieldDidBeginEditing:(UITextField *)textField;

被调用,启用这些按钮。

相关问题