在 AViewController 中有 initwithPDFAtPath
-(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;
}
尝试使用 viewDidLoad 方法在 BViewController 中打开 pdf 文件
- (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];
}
当我执行它时,它会因空数组消息而崩溃。由于未捕获的异常“NSRangeException”而终止应用程序,原因:“ * -[__NSArrayM objectAtIndex:]:索引 0 超出空数组的范围”
编辑:
AViewController 有一个 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];
}