在过渡到UIPageViewController时如何传递参数?

时间:2019-01-24 02:51:15

标签: ios swift

好的,所以我有了我的CollectionViewController,单击它,我就可以像这样转换到UIPageViewController:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    guard let selectedCell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: PhotoCell.self), for: indexPath ) as? PhotoCell
        else { fatalError("unexpected cell in collection view") }

    let asset = fetchResult.object(at: indexPath.item)
    self.navigationController?.pushViewController( PageViewController() , animated: true)
}

在我的PageViewController中:

override init(transitionStyle style: UIPageViewController.TransitionStyle, navigationOrientation: UIPageViewController.NavigationOrientation, options: [UIPageViewController.OptionsKey : Any]? = nil) {
    super.init(transitionStyle: .scroll, navigationOrientation: navigationOrientation, options: nil)
    delegate = self
    dataSource = self
    edgesForExtendedLayout = []

    setViewControllers([allDetailedViewControllers[0]], direction: .forward, animated: true, completion: nil)
}

我有一个setViewControllers方法,在其中设置了所有DetailedViewControllers [0]中的第一个。我的问题是,如何单击collectionView单元格,然后将适当的单元格索引或任何参数传递给页面viewcontroller,以便可以将setViewControllers与所需的索引一起使用。 例如:setViewControllers([allDetailedViewControllers[desiredIndex/argument]], direction: .forward, animated: true, completion: nil)

据我所知,UIPageController没有重写init(with parameter:argumentsType)方法,这就是为什么我卡住了。请帮忙!

1 个答案:

答案 0 :(得分:1)

在推送之前,您可以调用任何方法/为pageViewController设置任何属性。

例如,在您的PageViewController中编写此函数:

func doWhateverYouWant(_ arg:YourArgType)
{
    //do whatever you want with your args
    setViewControllers([allDetailedViewControllers[0]], direction: .forward, animated: true, completion: nil)
}

要推送它,请写下来:

let pageViewController = PageViewController()
pageViewController.doWhateverYouWant(yourArgs)
self.navigationController?.pushViewController( pageViewController , animated: true)