当我将数据从 TableView 传递给子 PageViewController 时,它可以转到下一个子
Posted
技术标签:
【中文标题】当我将数据从 TableView 传递给子 PageViewController 时,它可以转到下一个子【英文标题】:When I pass data from TableView to child PageViewController, it can go to next child 【发布时间】:2017-01-21 01:52:57 【问题描述】:我有一个UIViewController
和一个UITableView
。然后我用两个viewcontroller
创建一个PageViewController
。如果我将数据传递给孩子,Pageview
将不起作用。如果我改变了
setViewControllers([subjective], direction: .Forward, animated: true, completion: nil)
到
setViewControllers([firstVC], direction: .Forward, animated: true, completion: nil)
网页浏览有效,但数据未通过。我对此一无所知。请帮帮我。谢谢
代码工作:
网页浏览控制器类:
import UIKit
class PageVC : UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource
var lblhoten = String()
var lblngaysinh = String()
var lblsodt = String()
lazy var VCArr: [UIViewController] =
return [self.VCInstance("ThongTinBNPage2"),
self.VCInstance("ThongTinBNPage3")]
()
private func VCInstance(name: String) -> UIViewController
return UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier(name)
override func viewDidLoad()
super.viewDidLoad()
self.dataSource = self
self.delegate = self
if let firstVC = VCArr.first
let subjective = self.storyboard?.instantiateViewControllerWithIdentifier("ThongTinBNPage2") as! VCSubjective
subjective.lblhoten = lblhoten
subjective.lblngaysinh = lblngaysinh
subjective.lblsodt = lblsodt
setViewControllers([subjective], direction: .Forward, animated: true, completion: nil)
public func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
guard let viewcontrollerindex = VCArr.indexOf(viewController) else
return nil
let previousindex = viewcontrollerindex - 1
guard previousindex >= 0 else
return VCArr.last
guard VCArr.count > previousindex else
return nil
return VCArr[previousindex]
public func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
guard let viewcontrollerindex = VCArr.indexOf(viewController) else
return nil
let nextindex = viewcontrollerindex + 1
guard nextindex < VCArr.count else
return VCArr.first
guard VCArr.count > nextindex else
return nil
return VCArr[nextindex]
public func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
return VCArr.count
// The selected item reflected in the page indicator.
public func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
guard let firstviewcontroller = viewControllers?.first,
let firstviewcontrollerindex = VCArr.indexOf(firstviewcontroller) else
return 0
return firstviewcontrollerindex
VC主观:
import UIKit
class VCSubjective: UIViewController
@IBOutlet var hoten: UILabel!
@IBOutlet var ngaysinh: UILabel!
@IBOutlet var sodt: UILabel!
var lblhoten = String()
var lblngaysinh = String()
var lblsodt = String()
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
hoten.text = lblhoten
ngaysinh.text = lblngaysinh
sodt.text = lblsodt
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
【问题讨论】:
为什么我在这里有两个反对票?请评论至少一个拒绝任何人的理由!!! 两个用户投票结束了这个问题,因为它“不清楚你在问什么”。也许改写你的问题。什么是实际问题,你的问题是什么。出于明显的原因,请避免使用“不起作用”之类的术语。你可以edit your question here。 页面视图控制器是容器视图控制器。您的内容视图控制器在哪里? 把你的项目给我 @ThinhLe 你能给我你的项目吗?我已经在我的项目中实现了这一点。 【参考方案1】:这里的问题是你引用了两个完全不同的对象。
在viewDidLoad
中创建ThongTinBNPage2
viewController,然后将其添加到pageViewController 的viewControllers 属性中。但是,VCArr 中存储的对象是两个完全不同的 viewController。
让我们这样想:
-
当
viewDidLoad
被调用时,你创建viewController object #1
然后将 viewController object #1
分配给 pageViewController 的 viewControllers 对象,使 viewControllers = [object #1]
的值
在pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
和pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
中,您引用了VCArr 对象。在第一次调用 VCArr 时,它会懒惰地创建两个完全不同的视图控制器对象 [object #2, object #3]
要修复此代码,您需要执行以下操作:
if let firstVC = VCArr.first
let subjective = firstVC // DO NOT create different VC here
subjective.lblhoten = lblhoten
subjective.lblngaysinh = lblngaysinh
subjective.lblsodt = lblsodt
setViewControllers([subjective], direction: .Forward, animated: true, completion: nil)
【讨论】:
谢谢。我更新到 xcode 8 和 swift 3 然后就完成了。以上是关于当我将数据从 TableView 传递给子 PageViewController 时,它可以转到下一个子的主要内容,如果未能解决你的问题,请参考以下文章
将数据从 tableview 传递到另一个 tableview