4

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.

empee
  • 151
  • 1
  • 8
  • Maybe your view controller is being released on background and recreated on entering foreground. Then, textView.delegate would be nil. Can you test this? – Marcelo Mar 10 '13 at 15:53
  • I tested if it was nil in appDelegate's didBecomeActive method, it wasn't. I also tested setting the ViewController as delegate again in that method. – empee Mar 10 '13 at 16:03
  • Maybe your textView is becoming nil. Try to set the delegate in viewDidAppear: for example. – Marcelo Mar 10 '13 at 16:04
  • My textview is not becoming nil. I can set the text of it, and that works. It's only the delegate method that is not getting called. It's really weird. – empee Mar 10 '13 at 16:11
  • Post some code. I just created a new project with a `UITextView` and implemented `textViewDidChangeSelection:` for it. After selecting my text view I tapped the home button, then started the app again. The keyboard is still up and the method fires again. – memmons Mar 10 '13 at 16:18
  • I have a textview with a custom inputView of CGRectZero, and instead add text to the textview programmatically when buttons are clicked. But that shouldn't make any difference I think. But at least it is hopeful that it works for you.. – empee Mar 10 '13 at 16:30
  • Post your code. Without code we are just shooting in the dark. – memmons Mar 10 '13 at 18:27
  • Well, I was asking if someone had had the same problem, and I guess no one recognized the problem. I'll edit the question with some more code, and also answer with what I believe is a solution, or at least a work around, which I just discovered. – empee Mar 11 '13 at 19:39

2 Answers2

1

The UITextView delegate method textViewDidChangeSelection: will fire again when the app returns from the background. So something else must be going on in your app.

Just to make sure I created a simple app with 2 UITextView controls, set their delegate to be the view controller and added this bit of code:

- (void) textViewDidChangeSelection:(UITextView *)textView
{
    NSLog(@"Fire change selection.");
}

Works fine both before backgrounding the app and after coming back from backgrounding.

memmons
  • 40,222
  • 21
  • 149
  • 183
1

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.

empee
  • 151
  • 1
  • 8