Trying to assign a property to a UIScrollView class from UIViewController

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.pdfIndexSection in the ViewController, does it not retain the value in the ScrollView.

  • Is there a better way to implement this concept?

Answers


The problem is that in PDFScrollView you are accessing the fields pdfIndexSection and pdfIndexRow right in the initWithFrame method, but you are only setting their values after you call it.

In other words, your - (id)initWithFrame:(CGRect)frame in PDFScrollView should be rewritten as

// PDFScrollView
-(id)initWithFrame:(CGRect)frame 
pdfIndexRow:(int) pdfindexRow 
pdfIndexSection:(int)pdfIndexSection

and then in your PDFViewController you initialize it like

PDFScrollView *sv = [[PDFScrollView alloc] initWithFrame:[[self view] bounds] 
pdfIndexRow:detailIndexRow 
pdfIndexSection:detailIndexSection ];

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.


Need Your Help

How to auto-adjust text size on a multi-line TextView according to the view max dimensions?

android textview textwrapping typeface text-size

I've been searching for a way of auto fit a text inside a textview. Through my search I've found many solutions like:

Need Header row to show when scolling down page

javascript jquery html5 css3

I don't know what it is called when referencing CSS but in Excel, you can freeze panes (e.g. the header row) so that when you scroll down, you can still see the column headings of the data you are

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.