无法将数据从 UIViewController 传递到 UIPageViewController 和其他视图



我有一个指向PageViewControler的segue,它控制着两个ViewControler,虽然我认为我已经正确设置了它并且可以流畅地运行它,但我无法将数据传递给其他两个视图。 我错过了什么?
无论如何,这是我从单元格指向PageViewControler的续集:

class HomeViewController:  UIViewController, UITableViewDelegate, UITableViewDataSource {
if segue.identifier == "showDetails" {
let localDetails = segue.destination as! localDetails
let IndexPath = self.tableView.indexPathForSelectedRow!
let row = IndexPath.row
tableView.deselectRow(at: IndexPath, animated: true)
localDetails.index = row
let singleLocalInfo = restaurants[row]
localDetails.localName = singleLocalInfo.locale }}

我的页面视图控件:

class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
func newVc(viewController: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: viewController)
}
lazy var orderedViewControllers: [UIViewController] = {
return [self.newVc(viewController: "localDetails"),
self.newVc(viewController: "blueScreen")]
}()  etc,etc... }

最后是我需要播放数据的 viewControler:

class localDetails: UIViewController {
var localName: String = ""
etc... 
}

我不得不削减大部分代码,但你明白了。 谢谢

基本上...

在实例化并连接到页面视图控制器的类中,在此处设置属性,然后在各自的控制器中设置它们。

假设"showDetails"是页面视图控制器的 segue 标识符:

class HomeViewController:  UIViewController, UITableViewDelegate, UITableViewDataSource {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetails" {
let pageVC = segue.destination as! PageViewController
let IndexPath = self.tableView.indexPathForSelectedRow!
let row = IndexPath.row
tableView.deselectRow(at: IndexPath, animated: true)
pageVC.index = row
let singleLocalInfo = restaurants[row]
pageVC.localName = singleLocalInfo.locale
pageVC.blueValue = "the value for blueScreen"
}
}
}
class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
var index: Int = 0
var localName: String = ""
var blueValue: String = ""
func newVc(viewController: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: viewController)
}
lazy var orderedViewControllers: [UIViewController] = {
let v1 = self.newVc(viewController: "localDetails")
v1.localName = self.localName
let v2 = self.newVc(viewController: "blueScreen")
v2.blueValue = self.blueValue
return [v1, v2]
}()
//etc,etc...
}

相关内容

  • 没有找到相关文章

最新更新