Okay.. here is the whole scenario
The concept is to show multiple orders on a screen with user being able to slider left or right to see the next one. Once the order is accepted or declined the order view slides down and is replaced by the one next to it or previous if the order is at end.
I have a tabbar view that contains a simple UIScrollView and Couple of Buttons (Accept & Decline). In the ViewDidLoad I prepare an array of Orders, initialize ViewControllers (equal to total orders) with Nib files and add these these ViewController’s views to the ScrollView and Set the ScrollView’s content size etc.. (for vertical & horizontal scrolling)
When the user taps the AcceptButton an animation is performed sliding the Visible View in the UIScrollView down and then removing it from super view.
The animation code is performed as follows.
BOOL lastPage = NO;
BOOL ordersEmpty = NO;
if (self.views.count == 1){
ordersEmpty = YES;
lastPage = YES;
}
UIViewController *controller2;
if (self.views.count == page+1 && !ordersEmpty){
lastPage = YES;
controller2 = [self.views objectAtIndex:page-1];
} else if (!ordersEmpty){
controller2 = [self.views objectAtIndex:page+1];
}
[UIView animateWithDuration:1.5 animations:^{
CGRect frame = controller.view.frame;
frame.origin.y = frame.size.height + 100;
controller.view.frame = frame;
if (lastPage == NO){
CGRect frame2 = controller2.view.frame;
frame2.origin.x = frame.origin.x;
controller2.view.frame = frame2;
}else{
// [self.scrollView scrollRectToVisible:controller2.view.frame animated:YES];
}
} completion:^(BOOL value){
[controller.view removeFromSuperview];
[self.views removeObject:controller];
totalPages.text = [NSString stringWithFormat:@"%i", self.views.count];
// currentPage.text = [NSString stringWithFormat:@"%i",
self.pageControl.numberOfPages = self.views.count;
if (lastPage && !ordersEmpty){
[self.scrollView scrollRectToVisible:controller2.view.frame animated:YES];
lastPageMoving = YES;
}else if (ordersEmpty){
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
label.text = @"There are currently no notifications";
label.textAlignment = UITextAlignmentCenter;
label.center = CGPointMake(self.scrollView.frame.size.width/2, self.scrollView.frame.size.height/3);
[self.scrollView addSubview:label];
[label release];
ofPages.text = @"";
totalPages.text = @"";
currentPage.text = @"";
self.scrollView.contentSize = CGSizeMake(320, 267);
}
int i = 0;
for (UIViewController *c in self.views)
{
c.view.frame = CGRectMake(self.scrollView.frame.size.width * i, 0, c.view.frame.size.width, c.view.frame.size.height);
i++;
}
if (controller2 != nil)
{
if ([controller2 isKindOfClass:[CancelledController class]]) {
self.acceptButton.hidden = YES;
[self.declineButton setTitle:@"Close" forState:UIControlStateNormal];
[self.declineButton setTitle:@"Close" forState:UIControlStateHighlighted];
}else{
self.acceptButton.hidden = NO;
[self.declineButton setTitle:@"Decline" forState:UIControlStateNormal];
[self.declineButton setTitle:@"Decline" forState:UIControlStateHighlighted];
}
}
}];
The animations work fine and views removed are deallocated however
when the last view is accepted the application crashes with exc_bad_access exactly at [UIView animateWithDuration:animations:completion] method call.
NSZombies does not detect exact leak point / cause..
After extensive research, I think it might be due to using blocks .. the memory management is fine because if I were overreleasing anything, NSZombies would have indicated it.
(THE CODE WORKS FINE IN SIMULATOR THOUGH – NO ERRORS WHAT SO EVER)
I made a video to demonstrate how animations work.. It’s of the simulator so no error here however, when I run it on a device, application crashes as soon as I press the close button.
An interesting thing is if I close second order first and accept first order after, code works fine too =( Very Confusing Indeed..!!
http://www.mediafire.com/?w1up9g9u6w0ucrn
The project targets iOS 4.0
You can start by changing the declaration
UIViewController *controller2toUIViewController *controller2 = nil;, then it’s a good idea to use only properties, so the lines:would be replaced by calling accessor methods of this property and you and we will have an exact idea if the ivar
controlleris retained or not. This is valid for all other ivars you’re using:lastPageMoving,ofPages,totalPages,currentPageetc.. – just change them to properties and be sure about their proper memory management.