在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];
}
答案 0 :(得分:2)
从我在此处看到的情况看,您似乎在modelArray
的号码之前初始化totalPages
,因为这仅在initWithPDFAtPath
中设置。虽然我认为您可以(并且应该)重新组织代码,以便只有一个视图控制器必须管理页码,但您可以通过移动行来解决问题
AViewController *aViewController = [[AViewController alloc] initWithPDFAtPath:path];
在您初始化modelArray
之前的位置。
希望这有帮助!
答案 1 :(得分:1)
听起来我totalPages
无法初始化BViewController
。
同样不确定如何存储它,就像在AViewController
中调用super init
之前对此变量进行初始化一样,这可能导致丢失此值。
为了解决这个问题,我认为你需要做几件事。
首先在AViewContoller
中将init方法改为:
-(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;
}
并将totalPages存储在参数中。
比你需要做的BViewController
- (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];
}
但实际上这似乎是所有控制器架构的一些明显问题。