1

Sir On Tap Gesture I am hide Keyboard.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

They work fine. But when we edit textView second time. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil]; call multiple time. And they execute my code 5-6 time inside keyboardWillBeHidden. Is their any way they not call when textview begin edit.

- (void)textViewDidBeginEditing:(UITextView *)textView
{

    if ([textView.text isEqualToString:@"Type your message Here..."]) {
        textView.text = @"";
        textView.textColor = [UIColor grayColor]; //optional
    }

    [self scrollToBottomAnimated:YES];
  }
- (void)scrollToBottomAnimated:(BOOL)animated
{
    NSInteger rows = [tableViews numberOfRowsInSection:0];
    if(rows > 0) {
        [tableViews scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rows - 1 inSection:0]
                          atScrollPosition:UITableViewScrollPositionBottom
                                  animated:animated];
    }
}
Tejas Ardeshna
  • 4,105
  • 2
  • 16
  • 39
priyanka gautam
  • 367
  • 3
  • 15

1 Answers1

1

It depends upon where and how many times you are registering for keyboard notifications with NSNotificationCenter.

If you are adding observer in viewwillappear and remove observer in viewwilldisappear.

If you are adding observer in viewdidload then remove observer in dealloc call.

Just try to change the place in which you register for the notification. To make sure that only the viewController visible is the controller that receive the notification please register for the notification in vieWillAppear and remove the notification in viewWillDisappear.

That will also solve your problem of calling code multiple times.

Sandeep Ahuja
  • 921
  • 6
  • 15