开发者

What is the difference between setting the delegate in Interface Builder vs using setDelegate:?

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 开发者_Go百科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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜