Load childViewController to scrollview parent with a button

扶醉桌前 提交于 2020-01-06 06:58:21

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/20328891/load-childviewcontroller-to-scrollview-parent-with-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!