快速在 PageViewControllers 中传递数据

Posted

技术标签:

【中文标题】快速在 PageViewControllers 中传递数据【英文标题】:Passing data in PageViewControllers swift 【发布时间】:2019-07-01 14:02:56 【问题描述】:

我有一个页面控制器,我在其中添加了 UIViewControllers 并在每个视图控制器中显示一堆表单。我现在面临的问题是我需要获取每个表单中提供的数据并保存它,这是在最后一个视图控制器中完成的。我曾尝试使用委托,但在单击下一个按钮的那一刻,之前存储的值变为nil,并且只显示最新的VC 的值。如何在此文本字段中传递数据。如有任何帮助,将不胜感激。

我的代表

protocol NextDelegate: AnyObject 
    func next(pageIndex: Int, model: CreatePropertyModel)
    func previous(pageIndex: Int, model: CreatePropertyModel)

我如何创建 VC 数组

lazy var controllers: [UIViewController] = 
        let descVC = DescVC()
        descVC.delegate = self
        let priceVC = PriceVC()
        priceVC.delegate = self
        let featuresVC = FeaturesVC()
        featuresVC.delegate = self
        let picturesVC = PicturesVC()
        picturesVC.delegate = self
        return [descVC, priceVC, featuresVC, picturesVC]
    ()

模型示例

class CreatePropertyModel: DictionaryEncodable 
    var title: String?
    var desc: String?
    var property_type_id: Int?
    var property_sub_type_id: Int?
    var location_id: Int?
    var currency: String?
    var price: Int?

【问题讨论】:

【参考方案1】:

对于所有步骤,将其存储在单例中。


protocol Answer 
    var isDone: Bool  get 


class Answer1: Answer 
    static public let updatedNotification = Notification.Name("Answer1Updated")

    var name: String? 
        didSet 
            NotificationCenter.default.post(name: Answer1.updatedNotification, object: nil)
        
    

    var isDone: Bool 
        return name != nil
    


class Answer2: Answer 
    var age: Int?

    var isDone: Bool 
        return age != nil
    


class Form 
    static let shared = Form()

    var answers: [Answer] = [Answer1(), Answer2()]

    var isDone: Bool 
        return answers.allSatisfy  $0.isDone == true 
    

    private init() 

    func reset() 
        answers = [Answer1(), Answer2()]
    

    var answer1: Answer1? 
        return Form.shared.answers.filter  $0 is Answer1 .first as? Answer1
    

    var answer2: Answer2? 
        return Form.shared.answers.filter  $0 is Answer2 .first as? Answer2
    


然后,在您的视图控制器中,像这样读取/写入值。

class MyViewControllerForAnswer1: UIViewController 
    var answer: Answer1? 
        return Form.shared.answer1
    

    var name: String? 
        get 
            return answer?.name
        
        set 
            answer?.name = newValue
        
    

    override func viewDidLoad() 
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(answerUpdated(notification:)), name: Answer1.updatedNotification, object: nil)

    

    @objc func answerUpdated(notification: Notification) 
        // Update your content
    


【讨论】:

Answer1Answer2 是视图控制器吗? 我添加了模型示例 MyViewControllerForAnswer1Answer1 模型的视图控制器 我在 ParentVC 上有一个下一个按钮触发下一个

以上是关于快速在 PageViewControllers 中传递数据的主要内容,如果未能解决你的问题,请参考以下文章

《iOS Human Interface Guidelines》——Page View Controller

Pycharm在Ubuntu中添加快速启动图标

快速算法在一组范围中快速找到一个数字所属的范围?

是否可以在列表视图中始终启用快速滚动?

使用啥数据结构来快速存储文本文件,它有变量号。行数和变量号。在 C++ 中快速添加子元素?

xshell如何将快捷命令添加到快速命令集