Alertview:alertViewShouldEnableFirstOtherButton方法无法检测子视图textView

时间:2013-11-13 08:28:08

标签: ios uialertview

我创建了一个自定义警报视图,在其中查看uitextview,如屏幕截图所示。

alertview with subviewed textview

我想在textview为空时禁用Submit按钮,所以我编辑了alertViewShouldEnableFirstOtherButton方法:

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    if ( alertView.tag == 5000 ) {

    for(UIView *view in alertView.subviews) {
        if([view isKindOfClass:[UITextView class]]) {
            UITextView *textField = (UITextView *)view;

            if ( textField.text.length == 0 ) {
                return NO ;
            }
            return YES ;

        }
    }
}

else {

    return YES ;
}

}

最初,提交按钮是禁用的,这是正确的,但在我编辑(在textview上输入内容)后,提交按钮仍然被禁用,似乎alertViewShouldEnableFirstOtherButton只调用一次。

那么,在我在subviewed textview上输入内容之后,有人可以教我如何启用提交按钮吗?感谢您的大力帮助。

##########首先编辑

以下是有关alertview的完整代码,

- (void)addRemarkAlert {

alert = [[UIAlertView alloc]
                      initWithTitle:[NSString stringWithFormat:@"Remarks\n\n\n\n\n\n"]
                      message:nil
                      delegate:self
                      cancelButtonTitle: @"Cancel"
                      otherButtonTitles:@"Submit", nil ];

// textView to subview in the alertview
textView_onAlertView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
textView_onAlertView.delegate = self ;
textView_onAlertView.layer.borderWidth = 2.0f;
textView_onAlertView.layer.borderColor = [[UIColor darkGrayColor] CGColor];
textView_onAlertView.layer.cornerRadius = 10.0f;
textView_onAlertView.clipsToBounds = YES ;
textView_onAlertView.autocorrectionType = UITextAutocorrectionTypeNo ;
textView_onAlertView.autocapitalizationType = UITextAutocapitalizationTypeSentences ;
textView_onAlertView.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:12];
[alert addSubview:textView_onAlertView];

alert.tag = 5000 ;
[alert show];

}

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{

if ( alertView.tag == 5000 ) {

    for(UIView *view in alertView.subviews)
    {
        if([view isKindOfClass:[UIButton class]])
        {
            UIButton *aButton = (UIButton *)view;
            if([aButton.titleLabel.text isEqualToString:@"Submit"])
            {
                if ( textView_onAlertView.text.length> 0 )
                {
                    ((UIButton *) view).enabled = YES;

                }
                else
                {

                    ((UIButton *) view).enabled = NO;

                }
            }
        }
    }


    return NO;
}

else {

    return YES ;
}

}

1 个答案:

答案 0 :(得分:1)

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    NSLog(@"%d",tv.text.length);

    for(UIView *view in alertView.subviews)
    {
        if([view isKindOfClass:[UIButton class]])
        {
            UIButton *aButton = (UIButton *)view;
            if([aButton.titleLabel.text isEqualToString:@"done"])
            {
                if ( tv.text.length> 0 )
                {
                    ((UIButton *) view).enabled = YES;
                }
                else
                {
                    ((UIButton *) view).enabled = NO;
                }
            }
        }
    }

    return NO;
}

//////////////////////////////////////Entire code for your question

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addRemarkAlert];
}

- (void)addRemarkAlert
{
    alert = [[UIAlertView alloc]
             initWithTitle:[NSString stringWithFormat:@"Remarks\n\n\n\n\n\n"]
             message:nil
             delegate:self
             cancelButtonTitle: @"Cancel"
             otherButtonTitles:@"Submit", nil ];

    // textView to subview in the alertview
    textView_onAlertView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
    textView_onAlertView.delegate = self ;
    textView_onAlertView.layer.borderWidth = 2.0f;
    textView_onAlertView.layer.borderColor = [[UIColor darkGrayColor] CGColor];
    textView_onAlertView.layer.cornerRadius = 10.0f;
    textView_onAlertView.clipsToBounds = YES ;
    textView_onAlertView.autocorrectionType = UITextAutocorrectionTypeNo ;
    textView_onAlertView.autocapitalizationType = UITextAutocapitalizationTypeSentences ;
    textView_onAlertView.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:12];
    [alert addSubview:textView_onAlertView];

    alert.tag = 5000 ;
    [alert show];
}

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{

    if ( alertView.tag == 5000 ) {

        for(UIView *view in alertView.subviews)
        {
            if([view isKindOfClass:[UIButton class]])
            {
                UIButton *aButton = (UIButton *)view;
                if([aButton.titleLabel.text isEqualToString:@"Submit"])
                {
                    if ( textView_onAlertView.text.length> 0 )
                    {
                        ((UIButton *) view).enabled = YES;

                    }
                    else
                    {

                        ((UIButton *) view).enabled = NO;

                    }
                }
            }
        }

        return NO;
    }
    else
    {
        return YES ;
    }
}

- (void)textViewDidChange:(UITextView *)textView
{
    [self alertViewShouldEnableFirstOtherButton:alert];
}