I am attempting to drill down through a number of UITableViewControllers, eventually getting to a pdf file which is loaded based on what section and row the user selects. I am attempting to pass the section and row information to the PDFViewController (which works), but I am not able to pass the selected section and row information to a UIScrollView, which actually loads the PDF. I attempted to set a property when the PDFScrollView is instantiated, but that value is not being retained when the PDFScrollView is loaded.
Code from PDFViewController.m
#import "PDFViewController.h"
#import "PDFScrollView.h"
#import "ProtocolDetailViewController.h"
@implementation PDFViewController
@synthesize detailIndexRow;
@synthesize detailIndexSection;
- (void)loadView {
[super loadView];
// Log to check to see if detailIndexSection has correct value
NSLog(@"pdfVC section %d", detailIndexSection);
NSLog(@"pdfVc row %d", detailIndexRow);
// Create PDFScrollView and add it to the view controller.
PDFScrollView *sv = [[PDFScrollView alloc] initWithFrame:[[self view] bounds]];
sv.pdfIndexSection = detailIndexSection;
[[self view] addSubview:sv];
}
Now from PDFScrollView.m where pdfIndexSection does not retain the value assigned to it in the above code from detailIndexSection
#import "PDFScrollView.h"
#import "TiledPDFView.h"
#import "PDFViewController.h"
#import <QuartzCore/QuartzCore.h>
@implementation PDFScrollView
@synthesize pdfIndexRow;
@synthesize pdfIndexSection;
- (id)initWithFrame:(CGRect)frame
{
// Check to see value of pdfIndexSection
NSLog(@"PDF section says %d", pdfIndexSection);
NSLog(@"PDF row says %d", pdfIndexRow);
if ((pdfIndexSection == 0) && (pdfIndexRow == 0)) {
NSURL *pdfURL = [[NSBundle mainBundle] URLForResource:@"cardiacarrestgen.pdf" withExtension:nil];
pdf = CGPDFDocumentCreateWithURL((__bridge_retained CFURLRef)pdfURL);
}
else if ((pdfIndexSection == 0) && (pdfIndexRow == 1)) {
NSURL *pdfURL = [[NSBundle mainBundle] URLForResource:@"cardiacarrestspec.pdf" withExtension:nil];
pdf = CGPDFDocumentCreateWithURL((__bridge_retained CFURLRef)pdfURL);
}
pdfIndexSection and pdfIndexRow are both int and return 0 no matter what section or row is selected in didSelectRowAtIndexPath.
So two questions:
-
Why when I assign an int value to
sv.pdfIndexSectionin theViewController, does it not retain the value in theScrollView. -
Is there a better way to implement this concept?
The problem is that in PDFScrollView you are accessing the fields
pdfIndexSectionandpdfIndexRowright in the initWithFrame method, but you are only setting their values after you call it.In other words, your
- (id)initWithFrame:(CGRect)framein PDFScrollView should be rewritten asand then in your PDFViewController you initialize it like
The difference is that now you are passing the values in the init method, because you use them there. Another approach would be not to do the PDF loading logic in the initWithFrame method, but instead in a separate method. That way, you could keep initWithFrame simple, and have time to properly initialize any other fields you may had, before loading the PDF.