2011-11-29 22 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

相关问题