Have initwithPDFAtPath in AViewController
-(id)initWithPDFAtPath:(NSString *)path {
NSURL *pdfUrl = [NSURL fileURLWithPath:path];
thePDF = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfUrl);
totalPages = (int)CGPDFDocumentGetNumberOfPages(thePDF);
self = [super initWithNibName:nil bundle:nil];
return self;
}
Trying to open pdf file in BViewController using viewDidLoad method
- (void)viewDidLoad {
[super viewDidLoad];
//modelArray holds the page numbers
modelArray = [[NSMutableArray alloc] init];
for (int index = 1; index <= totalPages; index++) {
[modelArray addObject:[NSString stringWithFormat:@"%i", index]];
}
thePageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation: UIPageViewControllerNavigationOrientationHorizontal options:nil];
thePageViewController.delegate = self;
thePageViewController.dataSource = self;
thePageViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSString *path = [[NSBundle mainBundle] pathForResource:@"pages" ofType:@"pdf"];
NSURL *pdfurl = [NSURL fileURLWithPath:path];
PDFDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfurl);
AViewController = [[AViewController alloc] initWithPDFAtPath:path];
aViewController.page = [modelArray objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:aViewController];
}
When i execute it crashes with empty array message. Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
EDIT:
AViewController has a PDFScrollview
- (void)viewDidLoad
{
[super viewDidLoad];
// Create our PDFScrollView and add it to the view controller.
CGPDFPageRef PDFPage = CGPDFDocumentGetPage(thePDF, [_page intValue]);
pdfScrollView = [[PDFScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 925)];
[pdfScrollView setPDFPage:PDFPage];
[self.view addSubview:pdfScrollView];
}
It sounds to me that totalPages
could not be initialised in BViewController
.
Also Not sure how you store it, as in AViewController
you do initialisation of this variable before calling super init
which could lead to loosing this value.
To fix this I think you need do couple of things.
First in AViewContoller
change init method to something like this:
-(id)initWithPDFAtPath:(NSString *)path {
self = [super initWithNibName:nil bundle:nil];
if (self) {
NSURL *pdfUrl = [NSURL fileURLWithPath:path];
_thePDF = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfUrl);
_totalPages = (int)CGPDFDocumentGetNumberOfPages(thePDF);
}
return self;
}
and store totalPages in parameters.
Than in BViewController
you need to do
- (void)viewDidLoad {
[super viewDidLoad];
//modelArray holds the page numbers
thePageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation: UIPageViewControllerNavigationOrientationHorizontal options:nil];
thePageViewController.delegate = self;
thePageViewController.dataSource = self;
thePageViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSString *path = [[NSBundle mainBundle] pathForResource:@"pages" ofType:@"pdf"];
NSURL *pdfurl = [NSURL fileURLWithPath:path];
PDFDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfurl);
AViewController = [[AViewController alloc] initWithPDFAtPath:path];
modelArray = [[NSMutableArray alloc] init];
if (aViewController.totalPages > 0) {
for (int index = 1; index <= aViewController.totalPages; index++) {
[modelArray addObject:[NSString stringWithFormat:@"%i", index]];
}
aViewController.page = [modelArray objectAtIndex:0];
}
NSArray *viewControllers = [NSArray arrayWithObject:aViewController];
}
But really that seems some obvious problems with all Controllers Architecture.