Question

I have a subclass of NSViewController conforming to NSTextViewDelegate protocol and whose view is a subclass of NSView (obviously). I have an NSTextField text box on the nib and my subclass of NSViewController was its delegate. When I try to type in the text box it was throwing EXC_BAD_ACCESS, but when I removed the delegate link then I could type just fine. I wasn't handling any events yet, I just had the link. I've poked around similar threads but I think I'm still to n00bish to understanding how to work their solutions into my specific problem. I will need to have a delegate for my textview. How can I fix this?

Was it helpful?

Solution

Victory is mine!

This was the issue, in the best way I know how to describe it:

In my AppDelegate.m file I would instantiate myCustomViewController and add its view as a subview to the main view of my window, like such:

myCustomViewController *myCustomVC = [[myCustomViewController alloc] initWithNibName...];
[self.view addSubview:myCustomVC.view];

but I wasn't retaining myCustomViewController so when the method containing this code returned myCustomVC was cleaned up. Later, when I went to type in the textBox associated with myCustomVC's subview, it was sending the delegate message to the memory address of myCustomVC which had since been deallocated.

The solution was to drop a ViewController object into my main xib and hook it to a corresponding property on file's owner.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top