UITextView delegate class call when text is clicked? What's happening?

So, I saw this question: How do you enable the "delegate" UITextView output to a class that implements the UITextViewDelegate protocol?

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

I have a MyViewController that implements a UITextViewDelegate. In the interface builder for nib inside the view, I selected TextView and assigned it to delegate to the file owner (which is MyViewController.)

MyViewController implemented

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

Every time I test my application, as soon as I click on TextView, I get EXC_BAD_ACCESS crash. If I delete the delegate link in IB, then the keyboard will appear normally.

I also tried creating an IBOutlet UITextView for text view inside MyViewController and linking a TextView with this IBOutlet in File Owner. In viewDidLoad, I assign:

 myDescriptionTextField.delegate = self; 

But this also leads to the same release of EXC_BAD_ACCESS as soon as I click on TextView.

Inside XCODE at the top, when it resets the stack trace (I think this is what it is?), Where it crashes, it says:

objc_msgSend ??

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

... etc.

Does it help? Am I so lost in this matter? Everything seems to be correctly connected.

+4
source share
2 answers

I would check how the MyViewController object is created and managed (how you store the object in your application). It is possible that although your UITextView exists, as it becomes a subordinate of one of your UIView objects ( addSubview saves the subview), the MyViewController object MyViewController freed from memory after creating the hierarchy of your view,

 myDescriptionTextField.delegate = self; 

does not save self , since saving a delegate can cause a reference loop problem.

Check this, and if that does not solve the problem, try debugging with NSZombieEnabled, because it will tell you exactly where you are linking to the invalid object.

+5
source

And I have your problem!

I decide by passing a weak delegate instead of self

 __weak typeof(self) weakSelf = self; 

than use weakSelf !!

0
source

Source: https://habr.com/ru/post/1339579/


All Articles