gpt4 book ai didi

ios - 在 PageViewControllers 中快速传递数据

转载 作者:行者123 更新时间:2023-11-29 05:38:01 24 4
gpt4 key购买 nike

我有一个页面 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com