I am trying to create a UIScrollView programatically. I have a scrollView for that ViewController. Here is how I instantiate it in loadView:
CGRect fullScreenRect = [[UIScreen mainScreen] applicationFrame];
self.scrollView = [[UIScrollView alloc] initWithFrame:fullScreenRect];
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
I then some items onto it, and then add it to view in loadView as follows:
[self.scrollView addSubview:self.searchBar];
[self.scrollView addSubview:button];
[self.view addSubview:self.scrollView];
So far everything should be fine. However, in other functions, I will be adding ImageViews and then I will need to set the scrollview’s content size. The way I do it is just use [self.scrollView addToSubView:image] and when I want to edit the content size of the scrollview, I do it as follows:
// Calculate scroll view size
float sizeOfContent = 0;
for (int i = 0; i < [self.scrollView.subviews count]; i++) {
UIImageView *view =[self.scrollView.subviews objectAtIndex:i];
sizeOfContent += view.frame.size.height;
[view release];
}
// Set content size for scroll view
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, sizeOfContent);
I keep getting an EXC_BAD_ACCESS error in the main.m class.
I suspect that the problem is with retaining the actual scrollView since I think scrollView is not retaining the same object that is a subview of view.
Any suggestions? Thanks in advance!
If you have defined
self.scrollViewas@property(nonatomic, retain)then first wrong thing is the line;It sets the retain count of scrollView to 2, which is not what you expect.
allocretained properties like this,I think your BAD_ACCESS error comes from here;
Why are you
releasingview? You did notallocit, so there is no reason toreleaseit. Remove the[view release]line and check again.