I have a gridview when i click on any image i send current index of that gridview so i can see that image. and I am loading images from doucment.
below is my viewDidload code.
but what my problem is. when i click any gridview item . let say its index is 4. and i set it here in currentpage=4. so when i open this page. it shows me 4th image. but my page controller current page is 4 but i can see my image which was at index 1.
- (void)viewDidLoad {
[super viewDidLoad];
pageControlBeingUsed = NO;
UIImageView *subview;
//NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor orangeColor],[UIColor blueColor], nil];
for (int i = 0; i < (NSInteger)totalImagesInGrid; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
subview = [[UIImageView alloc] initWithFrame:frame];
//subview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[tableDataArray objectAtIndex:i]]];
NSString *name = [tableDataArray objectAtIndex:(NSUInteger)i];
NSFileHandle* myFileHandle = [NSFileHandle fileHandleForReadingAtPath:name];
UIImage* loadedImage = [UIImage imageWithData:[myFileHandle readDataToEndOfFile]];
subview.image = loadedImage;
[self.scrollView addSubview:subview];
[subview release];
}
//float count = [totalImagesInGrid floatValue];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 13, self.scrollView.frame.size.height);
self.pageControl.currentPage = (NSInteger)currentImage;
NSLog(@"currentPage %d",(NSInteger)currentImage);
self.pageControl.numberOfPages = (NSInteger)totalImagesInGrid;
NSLog(@"(NSInteger)totalImagesInGrid %d",(NSInteger)totalImagesInGrid);
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
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 = (NSInteger)currentImage + page;
NSLog(@"Page %d",page);
}
NSLog(@"scrollViewDidScroll");
}
- (IBAction)changePage {
// Update the scroll view to the appropriate page
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];
pageControlBeingUsed = YES;
}
this is my gridview and i m click on (1,1) image.

And this is my page control view. but thats not the image i have selected . this is (0,1) image but see at page control it is on index 2.

set page index with selectedIndex of gridview for example see bellow…
and in SecondView Class’s
viewDidLoad:just call your secondView class methodchangePageat last of the method like bellowi hope this help you..