Hi I am recently work with Leaves api and I have problem with this , I need to display a PDF file , and my ViewController class has a nib file , I implement the code base on API sample code but does not show anything ! Am I missing something ?
#import "BookViewController.h"
#import "Utilities.h"
@implementation BookViewController
/////EDITED//////////////
- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
if (self = [super initWithNibName:@"BookViewController" bundle:nil]) {
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("paper.pdf"), NULL, NULL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
}
return self;
}
- (void)dealloc {
CGPDFDocumentRelease(pdf);
[super dealloc];
}
- (void) displayPageNumber:(NSUInteger)pageNumber {
self.navigationItem.title = [NSString stringWithFormat:
@"Page %u of %u",
pageNumber,
CGPDFDocumentGetNumberOfPages(pdf)];
}
#pragma mark LeavesViewDelegate methods
- (void) leavesView:(LeavesView *)leavesView willTurnToPageAtIndex:(NSUInteger)pageIndex {
[self displayPageNumber:pageIndex + 1];
}
#pragma mark LeavesViewDataSource methods
- (NSUInteger) numberOfPagesInLeavesView:(LeavesView*)leavesView {
return CGPDFDocumentGetNumberOfPages(pdf);
}
- (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx {
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index + 1);
CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox),
CGContextGetClipBoundingBox(ctx));
CGContextConcatCTM(ctx, transform);
CGContextDrawPDFPage(ctx, page);
}
#pragma mark UIViewController
- (void) viewDidLoad {
[super viewDidLoad];
leavesView.backgroundRendering = YES;
[self displayPageNumber:1];
}
How are you instantiating your controller?
If you call
initWithNibName(as I suppose you do, since you say it has a nib file), you are totally skipping your localinitmethod (i.e., the one in BookController); therefore, the PDF handling is not initialized at all.Try renaming your init method to
initWithNibName, and it should work; or create aninitWithNibNameand make it call youinit(google for “designated constructor” to learn more about this pattern).Notice that the full signature of
initWithNibNameis:You have more options available, like creating the controller by calling
init(instead ofinitWithNibName) or even create a methodsetPDF, then, after you create the controller any way you like, you can call this method, which in turns callsinitialize, etc…EDIT: if you decide to instantiate manually the controller, don’t forget to define
loadView: