textViewShouldBeginEditing not called when not editable become to editable
Asked Answered
K

2

5

I encountered a problem on the UITextFieldDelegate, anyone can help me will be great appreciate. There is a UITextView, and I implemented its delegate. Normally all delegate methods called very good. But in this case:

  1. set the UITextView not editable.
  2. long-press the UITextView until the popup appear (copy, cut, ...).
  3. dismiss the popup, and set the UITextView editable
  4. tap the UITextView to make the UITextView enter editing mode.
  5. you will find the delegate method textViewShouldBeginEditing not called.

I need the textViewShouldBeginEditing be called to handle some UI changes. Anyone knows how to solve it? Thanks very much!

Test project: I created a simple test project which will NSLog the calls of the methods, you can test my case quickly, thanks! The source code is here: http://goo.gl/tGQS5

Komsomol answered 25/7, 2012 at 5:56 Comment(0)
I
7

Did you perhaps forgot to go:

myTextView.delegate = self;

?

Extra Information

In a recent app I released, I did something similar.

I disabled a UITextField so I could use a long press gesture on a UITableViewSectionHeader to edit the section header's name, and single tap section header to select it.

I had to put:

// groupName is my UITextField
groupName.userInteractionEnabled = YES;
[groupName becomeFirstResponder];

In my long press gesture recognizer callback method.

Ichnology answered 25/7, 2012 at 6:49 Comment(2)
Yes I have set it in story board. Normally all delegate methods are called. But when you long-press the textview in not-editable mode, and then change it to editable mode, textViewShouldBeginEditing will not get called.Komsomol
In your ViewController header file, do you have <UITextViewDelegate> or <UITextFieldDelegate> ?Ichnology
P
0

@Albert Zhang, "textViewShouldBeginEditing" will be called only when your textview becomes first responder. You can force it by doing the following.

- (IBAction)onSwitch:(id)sender{
    self.textView.editable = self.editableSwitch.on;

    if(self.textView.editable)
    {
        [[self textView]becomeFirstResponder];
    }
}
Plebiscite answered 25/7, 2012 at 6:24 Comment(3)
Thanks SRIKANTH, I've tried this but doesn't work. textViewShouldBeginEditing still not called if you long-press when not editable.Komsomol
You want to make textview editable right when you long press on textview?Plebiscite
No, what I want is to monitor the editting status of UITextView: will begin editting, did begin edtting, will end editting, did end editting. Previously the UITextField is not editable, and I want to toggle it into editting mode by some other control, maybe clicking a button. But when you long-pressed when not editable, and then toggle it into editting mode, the method textViewShouldBeginEditing will not get called.Komsomol

© 2022 - 2024 — McMap. All rights reserved.