1

In a controller I'm creating a UIScrollView. I'm trying to set this viewcontroller as the UISCrollview delegate and to implement the delegate's method in order to add (later) a UIPageControl.

I've read a bit, and found this link, this other link and other here on SO, and some useful tutorial all around the web, but I don't understand what I'm doing wrong. Everytime a scroll the UIScrollView, the app crashes with an EXC_BAD_ACCESS error.

Here's my .h file

#import <UIKit/UIKit.h>

@interface StatsViewController : UIViewController <UIScrollViewDelegate> {
     UIScrollView *scrollView;
     UIPageControl *pageControl;
}

@end

Then in my .m file, I'm creating the scrollview and trying to define the delegate method like this:

- (void)viewDidLoad {
     [super viewDidLoad];
     NSInteger boxWidth = self.view.frame.size.width;
     NSInteger boxHeight = 412;
    scrollView = [ [UIScrollView alloc] initWithFrame:CGRectMake(0, 0,      self.view.frame.size.width, boxHeight)];
    scrollView.pagingEnabled = TRUE;
    scrollView.delegate = self;

     NSInteger numberOfViews = 2;

    StatBreatheCounter *breatheCounter = [ [StatBreatheCounter alloc] init];
    breatheCounter.view.frame = CGRectMake(0, 0, boxWidth, boxHeight);
    [scrollView addSubview:breatheCounter.view];

    BreatheLocationViewController *breatheLocation = [ [BreatheLocationViewController alloc] init];
    breatheLocation.view.frame = CGRectMake(320, 0, boxWidth, boxHeight);
    [scrollView addSubview:breatheLocation.view];

     scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, boxHeight);
     [self.view addSubview:scrollView];

}

 - (void)scrollViewDidScroll:(UIScrollView *)sender {
     NSLog(@"RUNNING");
 }

...but every time I slide on the scroll view, the app is crashing. Now, I'm quite a n00b on Ojective-C, but I feel I'm missing something. Browsing around everything points on the fact that the delegate could be deallocated early, and when the user trigger the action, no one is handling the method (sorry for the explanation :)). ...but if the delegate it's the viewcontroller itself, how could it be deallocated?

As you can see, I'm quite confused :( Any help would be really appreciated

-- EDIT: I'm going to include here the solution founded thanks with your comments and answer. When I posted my question I was so convinced that the error was coming from the way I was creating the UIScrollView and setting its delegate that I didn't realize that the problem was (as everything was suggesting, btw :)) I was allocating the StateViewController in its parent without declaring any "strong" reference to it (again, sorry for the explanation, I'm really a n00b in this). Thanks a lot for your helping in pointing me on the right direction

Community
  • 1
  • 1
Sr.Richie
  • 5,680
  • 5
  • 38
  • 62
  • The crash could be in `viewDidLoad` of `BreatheLocationViewController`. Only a stacktrace can verify that. Can you please copy the crash message in console and paste it here. – iDev Nov 27 '12 at 20:34
  • @ACB thanks for your help.... but if a comment the line where i'm setting "scrollView.delegate = self", everything works, so I don't think the problem is in the other view controllers. Sorry for my extreme ignorance, but where can I find the information you suggest me to copy? In the debug navigator I've lot of info, is it enough the part relevant to thread 1, 0 objs_msgSend? – Sr.Richie Nov 27 '12 at 21:00
  • It should be there in your console. Just copy the place where it says crashed and paste it in your question. Check this. http://stackoverflow.com/questions/6462214/how-to-read-objective-c-stack-traces and http://stackoverflow.com/questions/3651204/how-to-print-exception-stack-trace-of-objective-c-exceptions-with-gnu-runtime-an Are you doing anything else in your scrollView delegate method? What happens if you just comment it out? – iDev Nov 27 '12 at 21:13

1 Answers1

2

It looks like you are losing reference to the delegate during scroll. I would look into any other release events around StatsViewController or other events that could cause it to be dereferenced.

jpgunter
  • 502
  • 4
  • 10