views:

309

answers:

1

I'm trying to set the delegate for a scroll view using Interface Builder.

If I have code like this:

MyScrollViewDelegate * delegate = [[MyScrollViewDelegate alloc] init];
[scrollView setDelegate:delegate];

in viewDidLoad, everything works perfectly.

If I open Interface Builder, add an NSObject and set the class to MyScrollViewDelegate, then link the scrollView's delegate to my instance of MyScrollViewDelegate and inspect [scrollView delegate] inside viewDidLoad, it looks like an instance of MyScrollViewDelegate, and I can interact with it, set proprerties etc, looks good.

However, when I scroll inside the scroll view I get an NSInvalidArgumentException:

 *** -[NSCFArray scrollViewDidScroll:]: unrecognized selector sent to instance 0x3d319a0
 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray scrollViewDidScroll:]: unrecognized selector sent to instance 0x3d319a0'

So, questions:

  1. What is the difference between setting the delegate in Interface Builder vs using setDelegate:?
  2. Why is "[NSCFArray scrollViewDidScroll:]" in the console, not, [MyScrollViewDelegate scrollViewDidScroll:]?
  3. What could I be doing wrong?
+2  A: 

There is no difference in setDelegate: itself.

However, you have a memory management issue. The problem is that objects don't retain their delegates (to avoid reference cycles). Your delegate is getting deallocated, and the memory is reused for an array. This is why you see a message intended for your delegate getting dispatched to an array.

You are seeing a difference because of an oddity of Interface Builder memory management. The "top-level objects", those objects that appear alongside File's Owner and Main Menu at the top level in the IB doc window, have an unbalanced retain on them. That's keeping your delegate alive in the IB case.

The solution is for something, perhaps your app delegate, to retain the scroll view delegate.

See Memory Management of Nib Objects.

Ken
Cheers, I've added an IBOutlet for the delegate.
Douglas