Search code examples
ioscocoa-touchuitextviewuitextviewdelegate

UITextView delegate method textViewDidChangeSelection not getting called when returning from background


My ViewController implements UITextView delegate method textViewDidChangeSelection. Everything works just as expected when testing it. However, if the app is put in the background, and then becomes active again, the delegate method is not getting called when changing the selection in the TextView. Anyone else who had this problem?

My UITextView subclass does this:

self.inputView = [[UIView alloc] initWithFrame:CGRectZero];

The above is in order for the keyboard not to show up, but at the same time keeping the TextView enabled.

The subclass also does this:

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
{
    if ( [UIMenuController sharedMenuController] )
    {
        [UIMenuController sharedMenuController].menuVisible = NO;
    }
    return NO;
}

This is in order not to show the copy paste pop up when clicking the UITextView. I think this method looks a bit weird, but I found it on SO a while ago, and it has done what it should.


Solution

  • What solved the problem for me was to call

    [myTextView reloadInputViews]
    

    In the AppDelegate method:

    -(void)applicationWillEnterForeground:
    

    I don't know exactly why it works, so if someone has any good explanation, that would be very appreciated.