gpt4 book ai didi

ios - 将每个 ChildViewController 上的数据传递到 UIPageViewController 上的下一个 ChildViewController

转载 作者:行者123 更新时间:2023-11-30 11:58:12 24 4
gpt4 key购买 nike

我有一个来自 Realm 的对象,我将每个控件分配给对象,我想要的是将每个 childVC 上分配的每个对象传递给下一个 childVC。

我在 UIPageViewcontroller 上初始化了该对象:

var artist = Artist()

我使用此函数滚动到 nextViewController :

func scrollToViewController(index newIndex: Int) {
if let firstViewController = viewControllers?.first,
let currentIndex = orderedViewControllers.index(of: firstViewController) {
let direction: UIPageViewControllerNavigationDirection = newIndex >= currentIndex ? .forward : .reverse
let nextViewController = orderedViewControllers[newIndex]
scrollToViewController(viewController: nextViewController, direction: direction)
}
}

我将每个文本框、标签分配给 childVC FirstViewController 上的该对象:

artist.artistName = textField.text!

下一个 childVC SecondViewvController 也是如此:

artist.genre = genres[0]

我想将我从前一个 VieController 分配的每个控制值传递给下一个 ViewController,例如:

在我的 SecondViewController 上,我希望将以前的值保存在 Artist.artistName 上,并分配我从标签获得的值。例如,只是打印 viewDidload 上的先前值。

class SecondViewController {
var artistNameFromFirstVC: String?
override func viewDidLoad() {
super.viewDidLoad()
print("ArtisName: \(artistNameFromFirstVC)")
// Setup Views
setupCollectionView()
}
...
}

我有超过 2 个子 Controller ,因此我想将每个先前的值传递给下一个 Controller 。

我很感激任何帮助。谢谢:)

enter image description here

最佳答案

有帮助吗?

struct Artist {
var name: String
var genre: String
}
class FirstViewController: UIViewController {
var name:String?
}
class SecondViewController: UIViewController {
var genre:String?
}
enum ScrollController: Int {
case first = 0
case second

init(with index: Int) {
self = ScrollController(rawValue: index) ?? .first
}
var controller: (Artist) -> UIViewController {
return { artist in
switch self {
case .first:
let controller = FirstViewController()
controller.name = artist.name
return controller
case .second:
let controller = SecondViewController()
controller.genre = artist.genre
return controller
}
}
}
}
func scrollToViewController(index newIndex: Int) {
if let firstViewController = viewControllers?.first,
let currentIndex = orderedViewControllers.index(of: firstViewController) {
let direction: UIPageViewControllerNavigationDirection = newIndex >= currentIndex ? .forward : .reverse
let nextViewController = ScrollController(with: newIndex).controller
scrollToViewController(viewController: nextViewController, direction: direction)
}
}

关于ios - 将每个 ChildViewController 上的数据传递到 UIPageViewController 上的下一个 ChildViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47555459/

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