ios - 在 PageViewControllers 中快速传递数据

标签 ios swift uipagecontrol

我有一个页面 Controller ,我在其中添加了 UIViewController 并在每个 View Controller 中显示一堆表单。我现在面临的问题是我需要获取每个表单中提供的数据并将其保存在最后一个 View Controller 中完成。我尝试过使用委托(delegate),但单击下一个按钮时,之前存储的值将变为 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?
}

最佳答案

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


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
    }
}

然后,在 View Controller 中,像这样读取/写入值。

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
    }
}

关于ios - 在 PageViewControllers 中快速传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56837061/

相关文章:

ios - phonegap inappbrowser IOS 不工作

swift - 如何跳过 for-in 循环的迭代(Swift 3)

ios - 如何每天更新一个标签文本,以便用户每天获得另一个预定义文本?

从非 View 快速请求地理定位

ios - UIPageViewController -presentationCount 未触发

ios - 使用图像数组进行页面控制

c++ - 使用多个框架给出 clang : error: linker command failed with exit code 1

iOS : 'MyViewController' does not conform to protocol 'UITableViewDataSource'

ios - 当我调用 api 时,我使用以下方法请给出解决方案,我通过 COCOAPOD 安装 Afnetwoking

objective-c - 页面控制按钮