Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView delegate class crashes on clicking the textview?! What's going on?

So I saw this question: How do you connect the "delegate" outlet of a UITextView to a class that implements UITextViewDelegate protocol?

My problem is similar to what booboo is describing in the second response (not the one marked as the answer.)

I have a MyViewController which implements UITextViewDelegate. In interface builder for the nib, inside the view I have selected the TextView and assigned it's delegate to the File Owner (which is MyViewController.)

MyViewController has implemented

- (void)textViewDidBeginEditing:(UITextView *)textView
{
NSLog(@"TextView EDIT %@",textView);
}

Every single time I test my app though, as soon as I click on the TextView I get an EXC_BAD_ACCESS crash. If I remove the delegate link in IB then the keyboard pops up fine.

I have also tried creating an IBOutlet UITextView to the textview inside MyViewController and linking the TextView to this IBOutlet in the File Owner. In the viewDidLoad then I assign:

myDescriptionTextField.delegate = self;

But this also results in the same issue of an EXC_BAD_ACCESS as soon as I click the TextView.

Inside XCODE at the top when it crashes the stack trace(i think this is what it is ?) of where it crashes says:

objc_msgSend ??

-[UIResponder becomeFirstResponder]
-[UITextView becomeFirstResponder]
-[UITextInteractionAssistant setFirstResponderIfNecessary]

... etc?

Does this help? I'm so lost on what this issue is? Everything seems linked correctly.

like image 792
skålfyfan Avatar asked Feb 13 '11 20:02

skålfyfan


1 Answers

I would check how the MyViewController object is created and managed (how you keep the object in your application). It is possible that while your UITextView exists as it becomes a subview of one of your UIView object (addSubview retains the subview), the MyViewController object itself is released from the memory after you build up your view hierarchy.

myDescriptionTextField.delegate = self;

does not retain self, as retaining a delegate may cause a problem of reference cycles.

Check this, and if it doesn't solve the problem try debugging with NSZombieEnabled because it will tell you where exactly you make a reference to an invalid object.

like image 130
MHC Avatar answered Sep 28 '22 09:09

MHC