Ios Alertview:alertViewShouldEnableFirstOtherButton方法可以';t检测子视图textView
我创建了一个自定义的alertview,子视图中包含一个uitextview,如屏幕截图所示 我想在textview为空时禁用Submit按钮,因此我编辑了alertViewShouldEnableFirstOtherButton方法:Ios Alertview:alertViewShouldEnableFirstOtherButton方法可以';t检测子视图textView,ios,uialertview,Ios,Uialertview,我创建了一个自定义的alertview,子视图中包含一个uitextview,如屏幕截图所示 我想在textview为空时禁用Submit按钮,因此我编辑了alertViewShouldEnableFirstOtherButton方法: - (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView { if ( alertView.tag == 5000 ) { for(UIView *view
- (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 ;
}
}
最初,Submit按钮是disable(禁用),这是正确的,但在我编辑(在textview上键入内容)后,Submit按钮仍然处于禁用状态,似乎AlertView应该只调用一次EnableFirstotherButton
那么,有人能教我在子浏览文本视图中键入内容后如何启用提交按钮吗?谢谢你的大力帮助
##########第一次编辑
下面是有关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 ;
}
}再次感谢您的回复,但这只会始终启用提交按钮,因为alertViewShouldEnableFirstOtherButton始终返回true在alertViewShouldEnableFirstOtherButton中编写代码。。。它会根据你的需要工作。试试上面的代码,我已经核对过了,它对我来说很好@ManLungChanThanks感谢您在这个问题上的巨大帮助,尽管我仍然没有找到工作来启用submit按钮。我放弃使用其他方法,当用户在文本视图为空的情况下按下提交按钮后,我会使用另一个alertview…如果您不介意在这里发布代码,我会对其进行更改。
- (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];
}