0

In the tableView:cellForRowAtIndexPath: implementation of my UITableViewController subclass I'm using this code snippet:

AlarmPicker *alarmPicker = [[AlarmPicker alloc] init];
alarmPicker.scrollViewDelegate = alarmPicker;
[cell.contentView addSubview:alarmPicker.view];

AlarmPicker implements various UIScrollViewDelegate methods.

In AlarmPicker.h I'm declaring scrollViewDelegate as follows:

@property (assign) id<UIScrollViewDelegate> scrollViewDelegate;

This is then being used in the implementation part of AlarmPicker:

UIScrollView *returnScrollView = [[UIScrollView alloc] init];
...
returnScrollView.delegate = self.scrollViewDelegate;

Unfortunately scrollViewDelegate is always null in this AlarmPicker instance so the implemented delegate methods don't get called at all.

A workaround would be to put the UIScrollViewDelegate method implementations into the class which does the alloc and init:

...
alarmPicker.scrollViewDelegate = self;
...

But that would be a very bad solution.

What do I have to do in order to tell the AlarmPicker instance that it should use itself as its delegate?

Patrick
  • 3,091
  • 4
  • 26
  • 29

1 Answers1

1

Is there some reason that you can't just write returnScrollView.delegate = self in your AlarmPicker class?

I don't see any reason why your scrollViewDelegate member would be nil here, unless the line returnScrollView.delegate = self.scrollViewDelegate; is happening in your init method, in which case you could move it to a later method or pass the required delegate in your AlarmPicker's init method.

Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
  • Thanks for your help. returnScrollView.delegate = self brings up an EXC_BAD_ACCESS when beginning to drag the UIScrollView. So I thought it would be a better idea to set the delegate from "outside". – Patrick Jun 30 '12 at 19:01
  • 1
    I would suggest turning on [zombies](http://stackoverflow.com/questions/2190227/how-do-i-setup-nszombieenabled-in-xcode-4/8863063) to track down that crash. If you can find the stacktrace for that crash, you could also post a question about that. – Jesse Rusak Jun 30 '12 at 21:03
  • Thank you, Jesse! Turning on zombies really helped to find the cause for this crash: `-[SDAlarmPicker respondsToSelector:]: message sent to deallocated instance: `-[AlarmPicker respondsToSelector:]: message sent to deallocated instance`. I think ARC is responsible for that. Now I'm using an instance variable to alloc and init the _AlarmPicker_ instance. Maybe that's not the finest solution, but it's working. – Patrick Jul 01 '12 at 09:42
  • You're welcome. That sounds like a fine solution to me. If my answer was helpful, you might consider accepting it; that will encourage others to answer your questions in the future. – Jesse Rusak Jul 01 '12 at 12:35