0

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

带有 subviewed textview 的 alertview

我想在 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 上输入内容后,有人可以教我如何启用提交按钮吗?感谢您的大力帮助。

##########第一次编辑

下面是关于警报视图的完整代码,

- (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 ;
}

}

4

1 回答 1

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];
}
于 2013-11-13T09:09:43.193 回答