Mujtaba Mujtaba - 5 months ago 38
Objective-C Question

how to use textview delegate when there are three textview in objective c

//Text View 1
textview1.layer.borderWidth = 1.0f;
textview1.layer.borderColor = [[UIColor blackColor] CGColor];
textview1.delegate=self;

UIToolbar* doneToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
doneToolbar.barStyle = UIBarStyleBlackTranslucent;
doneToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(donedescButtonClickedDismissKeyboard)],
nil];
[doneToolbar sizeToFit];
textview1.inputAccessoryView = doneToolbar;

//Text View 2

textview2.layer.borderWidth = 1.0f;
textview2.layer.borderColor = [[UIColor blackColor] CGColor];
textview2.delegate=self;

UIToolbar* doneToolbarmeasure = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
doneToolbarmeasure.barStyle = UIBarStyleBlackTranslucent;
doneToolbarmeasure.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(donemeasureButtonClickedDismissKeyboard)],
nil];
[doneToolbarmeasure sizeToFit];
textview2.inputAccessoryView = doneToolbarmeasure;

//Text View 3

textview3.layer.borderWidth = 1.0f;
textview3.layer.borderColor = [[UIColor blackColor] CGColor];
textview3.delegate=self;

UIToolbar* doneToolbarreportdesc = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
doneToolbarreportdesc.barStyle = UIBarStyleBlackTranslucent;
doneToolbarreportdesc.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(donereportdescButtonClickedDismissKeyboard)],
nil];
[doneToolbarreportdesc sizeToFit];
textview3.inputAccessoryView = doneToolbarreportdesc;

Delegate and Method

-(void)donedescButtonClickedDismissKeyboard
{
[textview1 resignFirstResponder];

}
-(void)donemeasureButtonClickedDismissKeyboard
{
[textview2 resignFirstResponder];
}
-(void)donereportdescdamageButtonClickedDismissKeyboard
{
[textview3 resignFirstResponder];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
textviewlbl1.hidden=YES;
textviewlbl2.hidden=YES;
textviewlbl3.hidden=YES;


}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if(textview1.text.length == 0)
{
textviewlbl1.hidden=NO;
}
if(textview2.text.length == 0)
{
textviewlbl2.hidden=NO;
}
if(textview3.text.length == 0)
{
textviewlbl3.hidden=NO;
}
}


When I click on Text view both the three textview label get hide and on unclick both three get unhide.
I need them to be hide and unhide them differently.
I have also use if condition but it no use.
when I use if-else if condition it only execute if condition not other condition.
Please Help!!

Answer

You can use same delegate method for all three textview like

-(void)textViewDidBeginEditing:(UITextView *)textView {
  if (textView == textview1) {
    // Code for textview1
  }
  else if (textView == textview2) {
    // Code for textview2
  }
  else if (textView == textview3) {
    // Code for textview3
  }
}

So your method will be like :

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView == textview1 && textview1.text.length == 0)
    {
        textviewlbl1.hidden=NO;
    }
    if(textView == textview2  && textview2.text.length == 0)
    {
        textviewlbl2.hidden=NO;
    }
    if(textView == textview3  && textview3.text.length == 0)
    {
        textviewlbl3.hidden=NO;
    }
}

Like this you can access all delegate methods of textview. Just differentiate textview by its object. Some other delegate methods are :

- (void)textViewDidChange:(UITextView *)textView;
- (void)textViewDidChangeSelection:(UITextView *)textView;
- (void)textViewDidEndEditing:(UITextView *)textView;
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange;
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)range;