0

In my iOS app, I have a view with a textField and a textView, both which are editable:

enter image description here

I want to scroll the view only when the user clicks on the textView (at the bottom) and not the textField. I set up a Notification for when the keyboard appears:

[[NSNotificationCenter defaultCenter] addObserver:self
  selector:@selector(keyboardWasShown:)
  name:UIKeyboardDidShowNotification object:nil];

But this notification gets triggered whenever the keyboard appears, regardless of what field was triggered. How do I only trigger the notification for the textField? I tried the following:

[[NSNotificationCenter defaultCenter] addObserver:self.stepDescriptionField
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification object:nil];

But then when I click on the textField, I get the error:

[UITextView keyboardWasShown:]: unrecognized selector sent to instance 0x13cd30950
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITextView keyboardWasShown:]: unrecognized selector sent to instance 0x13cd30950'
scientiffic
  • 9,045
  • 18
  • 76
  • 149

2 Answers2

1

I think you should use UITextViewDelegate Protocol instead of keyboard notifications, according to apple documentation you should use this method:

 - (void)textViewDidBeginEditing:(UITextView *)textView

"A text view sends this message to its delegate immediately after the user initiates editing in a text view and before any changes are actually made"

as an example:

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if (textView == self.stepDescriptionField) {
        [self.scrollView setContentOffset:CGPointMake(0 ,270) animated:YES];
    }
}

for more info check apple documentation at :

https://developer.apple.com/library/ios/documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html

Mostafa Abdellateef
  • 1,145
  • 14
  • 12
-1

This is how I ended up resolving the issue:

I add the notifiers:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeHidden:)
                                             name:UIKeyboardWillHideNotification object:nil];

Then I use textViewDidBeginEditing and textViewDidEndEditing to set the activeView:

// "Slides" screen up when textFields are being edited
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    NSLog(@"began editing textField");
    self.activeView = textView;
}

// "Slides" screen back down when textFields are no longer being edited
- (void)textViewDidEndEditing:(UITextView *)textView
{
    self.activeView = nil;
}

Then in my keyboardWasShown method, I check that the class of the activeview before scrolling:

if ( [NSStringFromClass(self.activeView.class) isEqualToString:@"UITextView"] && !CGRectContainsPoint(aRect, self.activeView.frame.origin) ) {
    NSLog(@"scrolling");
    CGPoint scrollPoint = CGPointMake(0.0, self.stepDescriptionField.frame.origin.y-kbSize.height);
    [self.scrollView setContentOffset:scrollPoint animated:YES];
}
scientiffic
  • 9,045
  • 18
  • 76
  • 149