2011-11-29 4 views
0

UITableViewControllers의 숫자로 드릴 다운하려고합니다. 결국 사용자가 선택한 섹션과 행을 기반으로로드 된 pdf 파일로 이동합니다. 섹션 및 행 정보를 PDFViewController (작동)에 전달하려고 시도했지만 실제로 선택한 PDF를로드하는 UIScrollView에 선택한 섹션 및 행 정보를 전달할 수 없습니다. PDFScrollView이 인스턴스화 될 때 속성을 설정하려고 시도했지만 PDFScrollView이로드 될 때 해당 값이 유지되지 않습니다. 이제 PDFViewController.mUIViewController에서 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 어디에서 모두 int 및 반환 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에서 위의 코드에 할당 된 값을 유지하지 않습니다 있습니다 pdfIndexSection0은 어떤 섹션이나 행이 didSelectRowAtIndexPath에서 선택 되더라도 상관 없습니다.

그래서 두 가지 질문 :

  • 나는 ViewControllersv.pdfIndexSection에 int 값을 할당 할 때, 그것은 ScrollView의 값을 유지하지 않는 이유.

  • 이 개념을 구현하는 더 좋은 방법이 있습니까?

답변

0

문제는 PDFScrollView 당신이 필드 오른쪽 initWithFrame 방법에 pdfIndexSectionpdfIndexRow에 액세스하는,하지만 당신은 단지 당신이 그것을 호출 후 그 값을 설정하는 것입니다. 즉

, 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

관련 문제