0

I have a page with 5 UITextView objects. I use the textViewDidBeginEditing: method to animate the observationComment text box which works fine. But now I want to do the same for the other 4, but I can't seem to work out how to determine which text view has been tapped. The observationComment textbox fires each time I click on any of the others.

-(void) textViewDidBeginEditing:(UITextView *) observationComment
{
    [self.view bringSubviewToFront:observationComment];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    obsCommentLabel.frame= CGRectMake(192, 113, 148, 21);
    observationComment.frame=CGRectMake(191, 135, 710, 250);
    [UIView commitAnimations];
} 
jszumski
  • 7,430
  • 11
  • 40
  • 53
RGriffiths
  • 5,722
  • 18
  • 72
  • 120
  • I don't quite understand. You have 5 UITextViews and 5 UILabels? And you want know which label belongs to the text view that was just clicked? – Eli Ganem Apr 30 '13 at 08:47
  • Yes but we can ignore the labels. The problem is that I have one UITextView is called observationComment. When I click inside this text view the animations work fine. Now I have added 4 more UITextViews and I would like to set different animations for each of them, however when I click on any of them the observationComment frame animates. I am trying to work out how to distinguish between the different UITextViews from within the textViewDidBeginEditing method. – RGriffiths Apr 30 '13 at 09:27
  • I believe this might be your best bet http://stackoverflow.com/questions/1823317/get-the-current-first-responder-without-using-a-private-api – RdPC Apr 30 '13 at 14:55

3 Answers3

1

The object passed to the delegate method is generic so like you say, it will be used for all text views.

- (void)textViewDidBeginEditing:(UITextView *)textView

To tell the text views apart you either need to keep a reference to the UITextView's (IBOutlet or otherwise) as a property and then compare textView to those properties or alternatively, set the tag property on each UITextView to unique integers and check the tags to tell which is which. Using

#define OBSERVATION_COMMENT_TAG 1001

and then checking OBSERVATION_COMMENT_TAG makes your code more readable than hard coding the tag constants within the code. To reference the tag from within the -textViewDidBeginEditing: delegate method, you would use the tag property on the view itself

- (void)textViewDidBeginEditing:(UITextView *)textView {
    NSInteger tag = [textView tag];
    switch (tag) {
        case OBSERVATION_COMMENT_TAG:
        {
            // observation comment text view
            break;
        }
        case ADDITIONAL_TEXTVIEW_TAG:
        {
            // additional text view
            break;
        }
    }
}
Andrew
  • 7,630
  • 3
  • 42
  • 51
  • I have added tags to each of the text boxes but can't seem to workout how to reference the tags from within the textViewDidBeginEditing method. – RGriffiths Apr 30 '13 at 07:18
  • I've updating my answer showing how to reference the tags in the delegate method. – Andrew Apr 30 '13 at 19:34
1

I don't like the following solution, but it will work for your conditions:

YourViewController.h:

UITextView *tv1, *tv2, *tv3, *tv4, *tv5;

YourViewController.m:

-(void) textViewDidBeginEditing:(UITextView *)observationComment
{
    if (observationComment == tv1)
        // animation 1
    else if (observationComment == tv2)
        // animation 2
    ... and so on ...
}
Eli Ganem
  • 1,479
  • 1
  • 13
  • 26
0

The tags solution worked perfectly:

-(void) textViewDidBeginEditing: (UITextView *) obsComment
{
if (obsComment.tag == 1)
{
... animations
}
else if (obsComment.tag == 2)
{
... animations
}

Thanks for everyone's advice and help.

RGriffiths
  • 5,722
  • 18
  • 72
  • 120