Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mistergreen2011

macrumors member
Original poster
Mar 23, 2011
36
0
Hi folks,
I'm stump on a pageControl problem.

I made a simple View Based Application with a pagescroll through several images. This work fine especially when you click left of right of the pageControl.


The problem is the app crashes with a 'SIGABRT' error when click on the pageControl in another app with the same code. The difference is this view is called through a TAB Bar while the other isn't. The crash goes away when I remove the 'action sent' on the pageControl but obvious, I need it to call the changePage method.

I'm not sure if it's a delegate issue or not.

I'll keep the code short. Thanks.

Code:
@interface ThirdViewController : UIViewController <UIScrollViewDelegate>{
    UIScrollView* scrollView;
    UIPageControl* pageControl;
    BOOL pageControlBeingUsed;
}

@property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl* pageControl;
- (IBAction)changePage:(id) sender;


@end

#import "ThirdViewController.h"


@implementation ThirdViewController

@synthesize scrollView;
@synthesize pageControl;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [scrollView release];
    [pageControl release];
    [super dealloc];
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    pageControlBeingUsed = NO;
    
    
    NSArray *imageName = [NSArray arrayWithObjects:@"co2_1.jpg", @"co2_2.jpg", @"co2_3.jpg", @"co2_4.jpg", @"magnesium.jpg", @"micro.jpg", @"nitrogen.jpg", @"phosophate3.jpg", @"phosphate2.jpg", @"phosphate1.jpg", @"potassium.jpg", nil];
    for (int i = 0; i < imageName.count; i++) {
        UIView *subview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[imageName objectAtIndex: i]]];
        subview.frame = CGRectMake(i * 320.0, 0.0, 320.0, 411.0);
        [self.scrollView addSubview:subview];
        [subview release];
    }
    
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * imageName.count, self.scrollView.frame.size.height);
    

}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    //NSLog(@"scrolled");
    // Update the page when more than 50% of the previous/next page is visible
    
    if (!pageControlBeingUsed) {
		// Switch the indicator when more than 50% of the previous/next page is visible
		CGFloat pageWidth = self.scrollView.frame.size.width;
		int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
		self.pageControl.currentPage = page;
	}
}

- (IBAction)changePage:(id) sender {
    // update the scroll view to the appropriate page
    NSLog(@"clicked");
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
    [self.scrollView scrollRectToVisible:frame animated:YES];
    
	// Keep track of when scrolls happen in response to the page control
	// value changing. If we don't do this, a noticeable "flashing" occurs
	// as the the scroll delegate will temporarily switch back the page
	// number.
	pageControlBeingUsed = YES;
    
    
}


*Edit*
Actually, I noticed scrollViewDidScroll:(UIScrollView *)sender never gets called. It is inherited from <UIScrollViewDelegate>. ScrollviewDidScroll does get called in the simple app with no TAB BAR. So I guess this scrollView being buried in another view won't work with the UIScrollViewDelegate. So where should this delegate be?


*UPDATE*
I read somewhere that using interface builder has it's limitations in terms of delegates interactions. So I got rid of the IB Tab bar and code it since in code you add the viewController into the tab bar and not the xib like in interface builder. It's working now.

Hmmm, I guess IB should only used for simple interactions.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.