-2

在 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];
  }
4

2 回答 2

2

从我在这里看到的,看起来你modelArray在你有一个数字之前进行了初始化totalPages,因为这只是设置在initWithPDFAtPath. 尽管我认为您可以(并且应该)重新组织代码,以便只有一个视图控制器必须管理页码,但您可以通过移动该行来解决问题

AViewController *aViewController = [[AViewController alloc] initWithPDFAtPath:path];

到您初始化的点之前modelArray

希望这可以帮助!

于 2013-07-16T18:34:45.030 回答
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];
    }

但实际上,所有控制器架构似乎都存在一些明显的问题。

于 2013-07-16T19:39:38.517 回答