2011-11-29 21 views
0

我試圖鑽取一些UITableViewControllers,最終得到一個基於用戶選擇的部分和行加載的pdf文件。我試圖將部分和行信息傳遞給PDFViewController(有效),但我無法將選定的部分和行信息傳遞給UIScrollView,它實際上加載了PDF。我試圖在PDFScrollView被實例化時設置一個屬性,但是當加載PDFScrollView時,該值不會被保留。從PDFViewController.m試圖從UIViewController分配一個屬性到UIScrollView類

#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]; 

} 

現在

代碼從PDFScrollView.m其中pdfIndexSection不會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); 

    } 

pdfIndexSectionpdfIndexRow保留在上面的代碼分配給它的值都是int和回報0無論在didSelectRowAtIndexPath中選擇了哪一部分或哪一行。

所以兩個問題:

  • 爲什麼當我在ViewController一個int值賦給sv.pdfIndexSection,是不是保留在ScrollView價值。

  • 有沒有更好的方法來實現這個概念?

回答

0

的問題是,在PDFScrollView您正在訪問的字段pdfIndexSectionpdfIndexRow就在initWithFrame方法,但是你只設置它們的值後你怎麼稱呼它。

換句話說,在PDFScrollView您- (id)initWithFrame:(CGRect)frame應該被改寫爲

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

,然後在PDFViewController初始化它像

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

所不同的是,現在你正在傳遞的值init方法,因爲你在那裏使用它們。另一種方法不是在initWithFrame方法中執行PDF加載邏輯,而是在單獨的方法中執行。這樣,您可以保持簡單的initWithFrame,並有時間在加載PDF之前正確初始化您可能擁有的任何其他字段。

+0

謝謝!這是非常有意義的,並糾正了這個問題。非常感激! – blueHula

相關問題