如何将数据从 UIPageViewController 传递到它的“子视图”之一?

Posted

技术标签:

【中文标题】如何将数据从 UIPageViewController 传递到它的“子视图”之一?【英文标题】:How do I pass data from a UIPageViewController to one of its "subviews"? 【发布时间】:2018-07-02 02:39:05 【问题描述】:

好的,这是我的问题:

我想通过多个视图控制器将字符串类型变量的值传递到我的 UIPageViewController 中的一个页面中。这是 UIPageViewController 的代码(从下面的 import UIKit 开始):

import UIKit

class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource 

    var recieveProdTime2 = String()     // 2nd view controller for productive time data to pass through

    lazy var orderedViewControllers: [UIViewController] = 
        return [self.newVC(viewController: "sbHome"), self.newVC(viewController: "sbPoints"), self.newVC(viewController: "sbShop")]
    ()

    var pageControl = UIPageControl()

    override func viewDidLoad() 
        super.viewDidLoad()

        self.dataSource = self
        if let firstViewController = orderedViewControllers.first 
            setViewControllers([firstViewController],
                               direction: .forward,
                               animated: true,
                               completion: nil)
        

        self.delegate = self
        configurePageControl()

        if recieveProdTime2 != "" 
            performSegue(withIdentifier: "segue3", sender: self)
        

    

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
        let destinationVC = segue.destination as! HomeViewController
        destinationVC.productive = recieveProdTime2
    

    func configurePageControl() 

        pageControl = UIPageControl(frame: CGRect(x: 0, y: UIScreen.main.bounds.maxY - 50, width: UIScreen.main.bounds.width, height: 50))
        pageControl.numberOfPages = orderedViewControllers.count
        pageControl.currentPage = 0
        self.view.addSubview(pageControl)

    

    func newVC(viewController: String) -> UIViewController 
        return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: viewController)
    

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? 

        guard let viewControllerIndex = orderedViewControllers.index(of: 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, viewControllerAfter viewController: UIViewController) -> UIViewController? 

        guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else 
            return nil
        

        let nextIndex = viewControllerIndex + 1

        guard orderedViewControllers.count != nextIndex else 
            return orderedViewControllers.first
        

        guard orderedViewControllers.count > nextIndex else 
            return nil
        

        return orderedViewControllers[nextIndex]

    

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

        let pageContentViewController = pageViewController.viewControllers![0]
        self.pageControl.currentPage = orderedViewControllers.index(of: pageContentViewController)!
    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    


如您所见,我正在实例化 3 个不同的视图。我想将recieveProdTime2 的值传递给HomeViewController,它是UIPageViewController 中的视图之一。这是 HomeViewController 的代码:

import UIKit

class HomeViewController: UIViewController 

    @IBOutlet weak var focusSession: UILabel!
    @IBOutlet weak var breakSession: UILabel!

    var productive = String()   // Productive time data from ViewController.swift arrives and is store

    override func viewDidLoad() 
        super.viewDidLoad()

        focusSession.text = productive

    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    


我使用 PageViewController 类中的“prepare(for segue:...)”方法并调用 HomeViewController 类中名为“productive”的变量来设置该类中名为“focusSession”的标签的文本。

问题

一切运行顺利,没有错误,项目构建并运行....但是标签的文本是空的!...它只是在模拟器中显示为一个空标签!我检查了错误框,我看到了这条消息:“警告:尝试在其视图不在窗口层次结构中的 Focus.PageViewController 上呈现 Focus.HomeViewController!”

如何将数据从 PageViewController 发送到 HomeViewController 并正确设置我的标签文本???

【问题讨论】:

你在哪里设置recieveProdTime2的值? @KarthickRamesh 从不同的视图控制器传入。该值很好...我通过在 viewDidLoad 中打印出 recieveProdTime2 的值来检查它是否来了,所以这部分很好 从您的警告中我可以得知 PageViewController 已从导航堆栈中删除。那么检查一下PageViewController是否仍然存在于内存中。 @KarthickRamesh 我该怎么做...对不起,我对此有点陌生 【参考方案1】:

试试这个:

将您的 orderedViewControllers 初始化替换为:

lazy var orderedViewControllers: [UIViewController] = 
    let sbPoint = self.newVC(viewController: "sbPoints") as! HomeViewController
    if recieveProdTime2 != "" 
        sbPoint.productive = recieveProdTime2
    
    return [self.newVC(viewController: "sbHome"), sbPoint , self.newVC(viewController: "sbShop")]
()

然后在recieveProdTime2 中输入一些值。 这里不需要使用prepareForSegue 所以删除:

if recieveProdTime2 != "" 
    performSegue(withIdentifier: "segue3", sender: self)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
    let destinationVC = segue.destination as! HomeViewController
    destinationVC.productive = recieveProdTime2

【讨论】:

好的,只是澄清一下,我正在尝试将 recieveProdTime2 的值传递给 sbHome 而不是 sbPoint...所以我将所有 sbPoint 替换为 sbHome 好吧,我按照你的建议做了,我得到一个“线程 1:信号 SIGABRT”错误...它突出显示 AppDelegate.swift 文件中的“class AppDelegate: UIResponder, UIApplicationDelegate ”行我的项目。 错误框消息是“以 NSException 类型的未捕获异常终止”..... 谷歌告诉我这是来自一些错误的插座连接或类似的东西,但我已经三重检查了我所有的连接和他们都很好......这以前没有发生过,我也没有建立任何新的联系 检查情节提要中的插座并删除 好的,这是因为我在情节提要 ID 中输入了一个类型,它实际上是“sbPoints”而不是“sbPoint”......非常感谢!!!!!!!你的解决方案成功了!!!!!!

以上是关于如何将数据从 UIPageViewController 传递到它的“子视图”之一?的主要内容,如果未能解决你的问题,请参考以下文章

UIPageViewController 与 UIViewControllerInteractiveTransitioning

iOS - 这是由 UISegmentedControl 控制的 UIPageViewController,还是这是啥?

如何将数据从 Ms 访问迁移到 Derby 数据库或如何将数据从 My sql 迁移到 Derby 数据库

如何将数据从mongodb导入hive

如何将数据直接从 Excel 插入 Oracle 数据库

如何将数据从活动发送到 java 类