我有一个PageViewController,它有两个UIViewControllers,它们的设置如下:
ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
self.pageViewController.delegate = self
let startingViewController = self.viewControllerAtIndex(self.index)
let viewControllers: NSArray = [startingViewController]
self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-10)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
}
在其中一个子视图控制器中,我有一个按钮,它应该转换到PageViewController中的特定索引:
@IBAction func createClick(sender: UIButton, forEvent event: UIEvent) {
let pageViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("NextViewController") as! UIViewController
let viewControllers: NSArray = [secondViewController]
pageViewController.self.setViewControllers(viewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
}
没有错误,但页面没有转换。我认为代表团存在问题,但我不确定如何修复它。
答案 0 :(得分:0)
问题是您实际上并未在应用实际使用的viewControllers
上设置UIPageViewController
属性 - 您正在从故事板中实例化一个全新的UIPageViewController
与屏幕上出现的任何控制器无关。
一种解决方案是在每个子视图控制器中创建对UIPageViewController
的引用,如下所示:
weak var pageViewController: UIPageViewController?
在dataSource
中,当您实施""之后"之前"方法,即:pageViewController:viewControllerBeforeViewController:
和pageViewController:viewControllerAfterViewController:
您需要创建新的子视图控制器,然后将pageViewController
属性设置为对" parent"的引用。 UIPageViewController
。
然后,当您调用@IBAction
方法时,可以像pageViewController
一样调用let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("NextViewController") as! UIViewController
let viewControllers: NSArray = [secondViewController]
pageViewController?.setViewControllers(viewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
上的方法:
\n
这样的事情应该有用。