Search code examples
iosuiscrollviewparent-childuipageviewcontrolleruipagecontrol

Load childViewController to scrollview parent with a button


I'm using the PageViewController (https://www.cocoacontrols.com/controls/pageviewcontroller) to create a Magazine App similar to Wired.

I've used this code on CustomPagerViewController to load the childview's:

- (IBAction)btn_index:(id)sender {

NSInteger currentPage = 3;
CGPoint offset = CGPointMake(currentPage * self.scrollView.frame.size.width, 0);
[self.scrollView setContentOffset:offset animated:YES];}

But I need several buttons on the indexviewcontroller to load the correct views and when I use the same code,

Property 'scrollView' not found on object of type 'Pag4_5ViewController *'

I've searched and tried several methods but all in vain.

Does anyone have any idea on how to solve this problem?


Solution

  • After talks in chat solution has been found and it is necessary to fix. The problem was that the self in this context (Pag4_5ViewController.m file) pointer to an instance of Pag4_5ViewController, and needed access to the methods of class CustomPagerViewController instance. After getting acquainted with the project found a solution. And one of the embodiments given below:

    Add in Pag4_5ViewController class (and to other classes) line:

    #import "CustomPagerViewController.h"
    

    and change code to something like this:

    - (IBAction)btn_index:(id)sender
    {
      CustomPagerViewController *parent = (CustomPagerViewController *)[self parentViewController];
      //page number where the transition will be accomplished
      NSInteger currentPage = 3;// 3 in Pag4_5ViewController.m file do nothing, because it is an index of the same page. But it be correct work for other pages.
      CGPoint offset = CGPointMake(currentPage * parent.scrollView.frame.size.width, 0);
      [parent.scrollView setContentOffset:offset animated:YES];
    }
    

    Note: This is just one of the possible variants of the code that will work. But there may be others.