1

I have spent quite a lot of time searching for this answer, but I'm estimating that it is my newbie status that is preventing me from seeing the light.

I am subclassing UIScrollView to make it into an infinite pager. As part of that, I need to see when a new page comes up, as I intend to only have three "pages" in use at a time for memory conservation. That means I need to have my subclass also act as a delegate.

I have followed the directions of: https://stackoverflow.com/a/9986842/773329. But I am running into some strange (to me) problems.

The primary one here is that when I override setDelegate:(id<UIScrollViewDelegate>), to insert the user's delegate, I get a loop in assembly that does not exit:

-(void)setDelegate:(id<UIScrollViewDelegate>)delegate {
    _selfDelegate->_userDelegate = delegate;
    super.delegate = nil;
    super.delegate = (id)_selfDelegate;
}

The assembly:

libobjc.A.dylib`objc_release:
0x12be090:  pushl  %ebp
0x12be091:  movl   %esp, %ebp
0x12be093:  subl   $8, %esp
0x12be096:  calll  0x12be09b                 ; objc_release + 11
0x12be09b:  popl   %ecx
0x12be09c:  movl   8(%ebp), %eax
0x12be09f:  testl  %eax, %eax
0x12be0a1:  je     0x12be0d5                 ; objc_release + 69
0x12be0a3:  movl   (%eax), %edx              ; <<< This is where the loop is
0x12be0a5:  movl   16(%edx), %edx
0x12be0a8:  andl   $-4, %edx
0x12be0ab:  testb  $2, 2(%edx)
0x12be0af:  je     0x12be0c5                 ; objc_release + 53
0x12be0b1:  movl   1002149(%ecx), %ecx
0x12be0b7:  movl   %ecx, 4(%esp)
0x12be0bb:  movl   %eax, (%esp)
0x12be0be:  calll  0x12bd08c                 ; objc_msgSend
0x12be0c3:  jmp    0x12be0d5                 ; objc_release + 69
0x12be0c5:  movl   %eax, (%esp)
0x12be0c8:  movl   $0, 4(%esp)
0x12be0d0:  calll  0x12bf9d0                 ; -[NSObject release]
0x12be0d5:  addl   $8, %esp
0x12be0d8:  popl   %ebp
0x12be0d9:  ret    

Is there something that might be causing the problem?

Community
  • 1
  • 1
Joseph at SwiftOtter
  • 4,276
  • 5
  • 37
  • 55
  • 1
    Any particular reason you need two delegates? Why not just implement your paging in the normal delegate, which should be an object you own. – aLevelOfIndirection May 03 '13 at 15:00
  • I'd even consider NOT using your own UIScrollView subclass; why not use a UICollectionView? – Wayne Hartman May 03 '13 at 15:36
  • 1
    A delegate is just a class that implements a particular interface. It generally should be implemented as a part of the class which has the best access to the relevant data, but you can make it a separate class that has to go elsewhere to look for the data if that seems to make sense. **There is absolutely nothing "special" about delegates.** – Hot Licks May 03 '13 at 15:38
  • would not "super.delegate = bla;" end up calling this setter recursively ? – YvesLeBorg May 03 '13 at 18:48
  • Don't subclass UIScrollView, and apple classes actually have multiple delegates in some cases. – Justin Meiners May 03 '13 at 19:15
  • Thank you so much. I have switched to using a `UICollectionView`, and that is already working great. @WayneHartman - if you want to put that in an answer, I will upvote/mark as answer. – Joseph at SwiftOtter May 03 '13 at 21:55

2 Answers2

2

Based upon what you are doing, I think a UICollectionView would fit perfectly with what you want to accomplish. This removes you from having to write your own code for reusing views and other things you would have to do in order to have performant code.

UICollectionView Reference

Wayne Hartman
  • 18,369
  • 7
  • 84
  • 116
1

You absolutely can and should implement infinite scrolling by using a regular delegate. Which particular functionality are you looking to override inside your subclass?

Alexei Sholik
  • 7,287
  • 2
  • 31
  • 41