PageViewController 嵌入到其他 PageViewController
Posted
技术标签:
【中文标题】PageViewController 嵌入到其他 PageViewController【英文标题】:PageViewController embedded in other PageViewController 【发布时间】:2016-01-31 15:28:14 【问题描述】:我在ViewController
中创建了一个PageViewController
,如下所示:
MainViewController (viewDidLoad())
self.pageViewController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
self.pageViewController.dataSource = self
self.pageViewController.delegate = self;
self.pageViewController.view.frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height)
let initialViewController : UIViewController! = viewControllerArray.objectAtIndex(self.currentPageIndex) as! UIViewController
let viewControllers: NSArray? = NSArray(objects: initialViewController)
pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated:true, completion: nil)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
self.syncScrollView()
一切都很好。这个viewController
在方法上捕获事件
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
我的问题是这个 pageViewController 的其中一个页面里面有其他 UIPageViewController,与其他 UIViewController 相关联。我创建另一个 UIPageViewController 的方式是:
SecondViewController (viewDidLoad()
)
self.pageNoticiaViewcontroller = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Vertical, options: nil)
self.pageNoticiaViewcontroller.didMoveToParentViewController(self);
self.pageNoticiaViewcontroller.dataSource = self;
self.pageNoticiaViewcontroller.delegate = self;
let startVC = self.getControllerAtIndex(0) as InicioIndividualUIViewController;
let viewControllers = NSArray(object: startVC);
self.pageNoticiaViewcontroller.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: true, completion: nil);
self.pageNoticiaViewcontroller.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height - 40);
self.addChildViewController(self.pageNoticiaViewcontroller);
self.view.addSubview(self.pageNoticiaViewcontroller.view);
问题是:
第二个 PageViewController (pageNoticiaViewController
)不调用方法presentationCountForPageViewController
,但调用方法presentationCountForPageViewController
父级
我怎么能做到第二个PageViewController
调用第二个 UIViewController 的方法presentationConuntForPageViewController
,而不是调用父级的方法?非常感谢!
【问题讨论】:
【参考方案1】:我认为你必须这样尝试:
self.pageViewController.view.tag=0
self.pageNoticiaViewcontroller.view.tag=1
并将条件放入数据源和委托方法中,如
示例:
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
if pageController.view.tag == 0
return yourcount // here self.pageViewController count
else
return yourcount // here self.pageNoticiaViewcontroller count
【讨论】:
以上是关于PageViewController 嵌入到其他 PageViewController的主要内容,如果未能解决你的问题,请参考以下文章
在 ScrollView 中嵌入 PageViewController - 以编程方式 Swift 5
嵌入在 containerView 中的 PageViewController 显示前一个视图的小边距
如何删除嵌入式 PageViewController 上的按钮栏?
我的 pageviewcontroller 手势是不是被拦截?