gpt4 book ai didi

ios - 警报 View : alertViewShouldEnableFirstOtherButton method can't detect subview textView

转载 作者:行者123 更新时间:2023-11-29 03:29:51 25 4
gpt4 key购买 nike

我创建了一个自定义警报 View ,其中包含一个 uitextview subview ,如屏幕截图所示。

alertview with subviewed textview

我想在 TextView 为空时禁用“提交”按钮,因此我编辑了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只调用一次。

那么,有人可以教我如何在 subview 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 ;
}

}

最佳答案

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

关于ios - 警报 View : alertViewShouldEnableFirstOtherButton method can't detect subview textView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19948836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com