gpt4 book ai didi

ios - RxSwift - 双向绑定(bind)以使用同一屏幕编辑和添加模型

转载 作者:行者123 更新时间:2023-11-30 11:58:07 27 4
gpt4 key购买 nike

我可以绑定(bind)UITextField.rx.textVariable<T> 。当我进入“创建”屏幕时,这效果很好。但是,假设我已经创建了一个模型并想在同一屏幕上编辑它,我该怎么做?

我还看到了很多关于 <-> 代表的中缀运算符运算符(operator)。如果我没有配置这个中缀运算符,我总是需要做大量的工作来实现双向绑定(bind)?

这是我的代码

struct TodoViewModel: CustomDebugStringConvertible {
let title = Variable<String?>("Initial title")
let description = Variable<String?>("")
let dueDate = Variable<Date?>(Date())
let done = Variable<Bool>(false)

var debugDescription: String {
get {
return
"""
// ======
Title: \(self.title.value ?? "Nil")
Description: \(self.description.value ?? "Nil")
Due Date: \(String(describing: self.dueDate.value))
Done: \(self.done.value)
"""
}
}
}

// ViewController
class AddTaskTableViewController: UITableViewController {
@IBOutlet weak var labelTitle: UILabel!
@IBOutlet weak var txtTitle: UITextField!
@IBOutlet weak var txtDescription: UITextField!
@IBOutlet weak var txtDate: UITextField!
@IBOutlet weak var switchDone: UISwitch!

var todo = TodoViewModel()
var disposeBag = DisposeBag()
let dateFormatter = DateFormatter()

override func viewDidLoad() {
super.viewDidLoad()

self.dateFormatter.locale = Locale(identifier: "en-US")

// Configuring reactivity
// Binds the UITextField's text value to my Model
// let _ = self.txtTitle.rx.text.bind(to: self.todo.title).disposed(by: self.disposeBag)

// ⬆⬇ Makes more sense they both being a two-way binding. This way I can edit and add items using the same screen.

// Binds my model's title to the UITextView's text
// let _ = self.todo.title.asObservable().bind(to: self.txtTitle.rx.text)
let _ = self.txtDate.rx.text.map{ strDate in
return self.dateFormatter.date(from: strDate!)
}.bind(to: self.todo.dueDate).disposed(by: self.disposeBag)
let _ = self.txtDescription.rx.text.bind(to: self.todo.description).disposed(by: self.disposeBag)
let _ = self.switchDone.rx.isOn.bind(to: self.todo.done).disposed(by: self.disposeBag)

let _ = self.todo.title.asObservable().bind(to: self.labelTitle.rx.text).disposed(by: self.disposeBag)
}
}

最佳答案

我很想拥有两个初始化程序:一个用于创建新的待办事项,另一个用于编辑现有的待办事项:

let title: Variable<String?>
let description: Variable<String?>
let dueDate: Variable<Date?>
let done: Variable<Bool>

init() {
self.title = Variable<String?>("Initial title")
self.description = Variable<String?>("")
self.dueDate = Variable<Date?>(Date())
self.done = Variable<Bool>(false)
}

init(todo: TodoModel) {
self.title = Variable<String?>(todo.title)
self.description = Variable<String?>(todo.description)
self.dueDate = Variable<Date?>(todo.dueDate)
self.done = Variable<Bool>(todo.done)
}

正如您所发现的,中缀 <->运算符将变量绑定(bind)到控件属性并订阅控件属性的更改以更新变量。它还捆绑了它们的一次性元素,使它们具有相同的生命周期,并确保当控件属性的订阅完成时所有东西都被处置。来源如下:

func <-> <T>(property: ControlProperty<T>, variable: Variable<T>) -> Disposable {
let bindToUIDisposable = variable.asObservable()
.bindTo(property)
let bindToVariable = property
.subscribe(onNext: { n in
variable.value = n
}, onCompleted: {
bindToUIDisposable.dispose()
})

return StableCompositeDisposable.create(bindToUIDisposable, bindToVariable)
}

如果您想避免使用 <->运算符,那么您需要自己为每个双向绑定(bind)执行所有这些操作。

希望有帮助。

关于ios - RxSwift - 双向绑定(bind)以使用同一屏幕编辑和添加模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47564810/

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