Reputation: 6051
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];
}
Upvotes: 1
Views: 425
Reputation: 69037
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 local init
method (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 an initWithNibName
and make it call you init
(google for "designated constructor" to learn more about this pattern).
Notice that the full signature of initWithNibName
is:
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
You have more options available, like creating the controller by calling init
(instead of initWithNibName
) or even create a method setPDF
, then, after you create the controller any way you like, you can call this method, which in turns calls initialize
, etc...
EDIT: if you decide to instantiate manually the controller, don't forget to define loadView
:
- (void)loadView {
[super loadView];
if (leavesView) {
leavesView.frame = self.view.bounds;
leavesView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.view addSubview:leavesView];
}
}
Upvotes: 1