简体   繁体   中英

UITableViewController with UIPageViewController in UIViewController

What I m Thinking to do is ? there is one MainViewController in which there are two UIViews one loaded UITableViewController and other UIPageViewController . In TableViewController , there are list of pdf's names. But when i click of the pdf list, it does not loads the PageViewController with new pdf in context.

Pdf file names are store in .plist file, Pdfs are fetch from NSBundle or NSDocumentDirectory

In TableViewController

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath 
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];    
        Play *play = (Play *)[[self.sectionInfoArray objectAtIndex:indexPath.section] play];
        Quotation *qute = [play.Details objectAtIndex:indexPath.row];
        if ([qute.Key isEqualToString:@"ABC"])
        {
            AppDelegate *app = [[UIApplication sharedApplication]delegate];
            app.strpdfname = qute.Value;
            MiddleViewController *mid = [[MiddleViewController alloc] initWithNibName:@"MiddleViewController" bundle:nil];
            ContentViewController *cvc = [[ContentViewController alloc] initWithNibName:@"ContentViewController" bundle:nil];
            [mid viewDidLoad];
            [cvc viewDidLoad];
        }
        else
        {
            MainViewController *vc = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
            [vc viewDidLoad];
        }
    }

In PageViewController
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.modelArray = [[NSMutableArray alloc] init];

        AppDelegate *app = [[UIApplication sharedApplication] delegate];
        self.pdfName = app.strpdfname;

        NSString *path = [[NSBundle mainBundle] pathForResource:pdfName ofType:nil];
        NSURL *targetURL = [NSURL fileURLWithPath:path];

        originalPDF = CGPDFDocumentCreateWithURL((__bridge CFURLRef)targetURL);
        numberOfPages = CGPDFDocumentGetNumberOfPages(originalPDF);

        for (int index = 0; index < numberOfPages ; index++)
        {
            [self.modelArray addObject:[NSString stringWithFormat:@"%d",index]];
        }

        self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl                                               navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
        self.pageViewController.delegate = self;
        self.pageViewController.dataSource = self;
        contentViewController = [[ContentViewController alloc] initWithNibName:@"ContentViewController" bundle:nil];
        contentViewController.labelContents = [self.modelArray objectAtIndex:0];
        NSArray *viewControllers = [NSArray arrayWithObject:contentViewController];
        [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward
                                       animated:NO 
                                     completion:nil];
        [self addChildViewController:self.pageViewController];
        [self.view addSubview:self.pageViewController.view];
        [self.pageViewController didMoveToParentViewController:self];       
        CGRect pageViewRect = self.view.frame;
        pageViewRect = CGRectInset(pageViewRect, 0.0, 0.0);
        self.pageViewController.view.frame = pageViewRect;
        self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
    }

    - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController 
      viewControllerBeforeViewController:(UIViewController *)viewController
    {
        NSUInteger currentIndex = [self.modelArray indexOfObject:[(ContentViewController *)viewController labelContents]];
        if(currentIndex == 0)
        {
            return nil;
        }
        contentViewController = [[ContentViewController alloc] init];
        contentViewController.labelContents = [self.modelArray objectAtIndex:currentIndex - 1];
        return contentViewController;
    }

    - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
       viewControllerAfterViewController:(UIViewController *)viewController
    {
        NSUInteger currentIndex = [self.modelArray indexOfObject:[(ContentViewController *)viewController labelContents]];
        if(currentIndex == self.modelArray.count-1)
        {
            return nil;
        }
        contentViewController = [[ContentViewController alloc] init];
        contentViewController.labelContents = [self.modelArray objectAtIndex:currentIndex + 1];
        return contentViewController;
    }

    - (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController
                   spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
    {
        UIViewController *currentViewController =   [self.pageViewController.viewControllers objectAtIndex:0];
        NSArray *viewControllers = [NSArray arrayWithObject:currentViewController];
        [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
        self.pageViewController.doubleSided = NO;
        return UIPageViewControllerSpineLocationMin;
    }

In ContentViewController of PageViewController
    -(UIImage*) imageFromPDF:(CGPDFDocumentRef)pdf withPageNumber:(NSUInteger)pageNumber withScale:(CGFloat)scale
    {
        CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdf,pageNumber);
        CGRect tmpRect = CGPDFPageGetBoxRect(pdfPage,kCGPDFMediaBox);
        CGRect rect =   CGRectMake(tmpRect.origin.x,tmpRect.origin.y,tmpRect.size.width*scale,tmpRect.size.height*scale);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(context,-50,rect.size.height-30);
        CGContextScaleCTM(context,scale,-scale);
        CGContextDrawPDFPage(context,pdfPage);
        UIImage* pdfImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return pdfImage;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        AppDelegate *app = [[UIApplication sharedApplication] delegate];
        self.pdfName = app.strpdfname;
        i = [labelContents intValue];    
        NSString *path = [[NSBundle mainBundle] pathForResource:pdfName ofType:nil];
        if (i!=0)
        {
            CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:path]);
            UIImage *img = [[UIImage alloc] init];
            img = [self imageFromPDF:pdf withPageNumber:i withScale:1];
            UIImageView *imgview1 = [[UIImageView alloc] initWithImage:img];
            [self.view addSubview:imgview1];
        }
        else 
        {
            UIImage *imgmain = [UIImage imageNamed:@"Diary4.png"];
            UIImageView *imgvmain = [[UIImageView alloc] initWithImage:imgmain];
            imgvmain.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y,580,self.view.frame.size.height);
            [self.view addSubview:imgvmain];
        }   
    }

In MainViewController

-(void)viewDidload
    {
        leftView = [[UIView alloc] initWithFrame:CGRectMake(5,25, 200,715)];
        leftView.backgroundColor = [UIColor clearColor];    
        tvc = [[TableViewController alloc] initWithStyle:UITableViewStylePlain];
        tvc.plays = self.plays;
        tvc.view.frame = leftView.frame;
        leftView.clipsToBounds = YES;
        [leftView addSubview:tvc.view];
        [self.view addSubview:leftView];
        middleView = [[UIView alloc] initWithFrame:CGRectMake(107, 30, 575, 670)];
        mvc = [[MiddleViewController alloc] initWithNibName:@"MiddleViewController" bundle:nil];
        [mvc.view setFrame:middleView.frame];
        [middleView addSubview:mvc.view];
        [self.view addSubview:middleView];
    }

As there is no reload data method for UIPageViewController . I was using UIPageViewController in UIViewController instead of that I use UINavigationController in which the UIPageViewController can be reloaded with dynamic data.So on click of UITableViewController , data is sends to UINavigationController --> UIPageViewController . This has solved the above issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM