gpt4 book ai didi

ios - 从 pageViewController 获取 ParentViewController

转载 作者:行者123 更新时间:2023-11-29 00:58:02 25 4
gpt4 key购买 nike

我有一个基于页面的应用程序,由每个 ViewController 中的 collectionViews 组成。然后我有一个 viewController 从 pageView 上的 barButton 出来,它导致一个添加页面供用户添加他们的数据。我试图找到一种方法来确定源 View Controller 是什么,以便我可以附加和重新加载正确的 ViewController。下面是我的 UIPageView 和 AddSubject 类:

添加主题.swift:

import UIKit
import ChameleonFramework


class AddSubjectView: UIViewController {

// collectionView
@IBOutlet var colourPicker: UICollectionView!

var source = UIViewController()

// source of colours to use in the colour collectionView
private var data = collectionViewColors.createColor()

override func viewDidLoad() {
super.viewDidLoad()

// gesture recognizer to determine when the user has tapped anywhere but the text field

let tap: UIGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(AddSubjectView.dismissKeyboard))
view.addGestureRecognizer(tap)


}

@IBAction func addPressed(sender: AnyObject) {




}

func addData() {




}

// dismiss keyboard when the user taps away from the text field (called above with #selector)
func dismissKeyboard() {

view.endEditing(true)
}

// mandatory methods to display collectionView data in the collectionView
}
extension AddSubjectView: UICollectionViewDataSource {

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

return data.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! colourCell



cell.data = self.data[indexPath.item]


return cell
}



func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

let cell = collectionView.cellForItemAtIndexPath(indexPath)

}



}

PageViewController.swift:

import UIKit
import ChameleonFramework

class PageViewController: UIPageViewController,UIPageViewControllerDelegate {

// created for use in the function which determines the title of the navigationBar
var arrayIndex: Int = 0
// reference for later function to determine title of navigationBar
var pageControl = UIPageControl.self

// NSUserDefaults
let defaults = NSUserDefaults.standardUserDefaults()

// set of viewControllers (based on their storyboard ID)

private(set) lazy var orderedViewControllers: [UIViewController] = {
return [self.newDayViewController("monday"),
self.newDayViewController("tuesday"),
self.newDayViewController("wednesday"),
self.newDayViewController("thursday"),
self.newDayViewController("friday"),
self.newDayViewController("saturday"),
self.newDayViewController("sunday")
]
}()


override func viewDidLoad() {
super.viewDidLoad()


self.setNeedsStatusBarAppearanceUpdate()
// setting the datasource & Delegate of the UIPageViewController
dataSource = self
delegate = self




// set the first viewController for the pageView (monday as it is the first in the set)

if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .Forward,
animated: true,
completion: nil)


}

}





// function to add view controllers
// function will only instantiateViewControllers which have a storyboard id containing 'day' e.g Monday, Tuesday, Wednesday

private func newDayViewController(day: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewControllerWithIdentifier("\(day)")
}




// set the navigationBar title to the dayViewController's title
// if the user is looking at orderedViewControllers[1], then the title of that day is "Tuesday"

func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if (!completed) {
return
}


if let firstViewController = viewControllers?.first,
let arrayIndex = orderedViewControllers.indexOf(firstViewController) {
switch arrayIndex {
case 0:
self.navigationController!.navigationBar.topItem!.title = "Monday"
self.navigationItem.title = "Monday"
break

case 1:
self.navigationController!.navigationBar.topItem!.title = "Tuesday"
self.navigationItem.title = "Tuesday"
break

case 2:
self.navigationController!.navigationBar.topItem!.title = "Wednesday"
self.navigationItem.title = "Wednesday"
break

case 3:
self.navigationController!.navigationBar.topItem!.title = "Thursday"
break
case 4:
self.navigationController!.navigationBar.topItem!.title = "Friday"
break

case 5:
self.navigationController!.navigationBar.topItem!.title = "Saturday"
break

case 6:
self.navigationController!.navigationBar.topItem!.title = "Sunday"
break

default:
self.navigationController!.navigationBar.topItem!.title = "Timetable"


}
}
}
}
// Mandatory functions and setup for the pageViewController to work

extension PageViewController: UIPageViewControllerDataSource {


func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return orderedViewControllers.last
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}

func pageViewController(pageViewController: UIPageViewController,
viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count


guard orderedViewControllersCount != nextIndex else {
return orderedViewControllers.first
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
}
// set number of viewControllers to be presented
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return orderedViewControllers.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
guard let firstViewController = viewControllers?.first,
firstViewControllerIndex = orderedViewControllers.indexOf(firstViewController) else {
return 0
}
return firstViewControllerIndex
}
}

我想知道是否有一种方法可以使用一个 addSubject View ,而不必为所有单独的 viewController 创建 barButton 项目并将它们链接到他们自己的单独 viewController。

任何帮助将不胜感激!

最佳答案

要在您的 UIPageViewController 中获取当前屏幕上的 UIViewController,您可以实现 UIPageViewControllerDelegate 协议(protocol)方法“didFinishAnimating”。我在我的一个应用程序中使用以下示例来实现此目的。

 func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {

let currentVC = pageViewController
.viewControllers![pageViewController.viewControllers!.count - 1]
}

如果您需要索引,最简单的方法是创建您自己的带有索引属性的 UIViewController 子类。然后在你的“viewControllerBeforeViewController”和“viewControllerAfterViewController”方法中,可以在返回ViewController之前分配索引

例子:

 func pageViewController(pageViewController: UIPageViewController,      viewControllerBeforeViewController viewController: UIViewController) ->   UIViewController? {

let currentDetailVC: RAFeedDetailViewController = viewController as! RAFeedDetailViewController

return vcAtIndex(currentDetailVC.index - 1)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

let currentDetailVC: RAFeedDetailViewController = viewController as! RAFeedDetailViewController

return vcAtIndex(currentDetailVC.index + 1)
}

func vcAtIndex(index: Int) -> RAFeedDetailViewController? {

guard index < self.dataSourceItems.count - 1 && index >= 0 else { return nil }


/* insert logic here to get the correct view controller */
let detailVC: RAArticleDetailViewController = self.storyboard?.instantiateViewControllerWithIdentifier(String(RAArticleDetailViewController.self)) as! RAArticleDetailViewController

detailVC.index = index

return vc
}

关于ios - 从 pageViewController 获取 ParentViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37413522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com