我绑定UIBindingObserver
barTintColor
新UIColor
和title
,但它没有显示。有线的是,当我拖回UIViewController并释放所有内容时
extension Reactive where Base: UINavigationBar {
var barTintColor: UIBindingObserver<Base, UIColor> {
return UIBindingObserver(UIElement: self.base) { navigationBar, barTintColor in
navigationBar.barTintColor = barTintColor
}
}
}
class BaseViewController: UIViewController {
private(set) var tintColor: Variable<UIColor> = Variable(ColorConstants.tintColor)
override func viewDidLoad() {
super.viewDidLoad()
if let navigationBar = self.navigationController?.navigationBar {
self.tintColor.asObservable()
.bind(to: navigationBar.rx.barTintColor)
.addDisposableTo(self.disposeBag)
}
}
}
class TasksListViewController: BaseViewController {
var viewModel: TasksListViewModel!
...
override func viewDidLoad() {
super.viewDidLoad()
self.viewModel.colorObsrvable
.unwrap()
.bind(to: self.tintColor)
.addDisposableTo(self.disposeBag)
self.viewModel.titleObsrvable
.unwrap()
.bind(to: self.rx.title)
.addDisposableTo(self.disposeBag)
}
}
class TasksListViewModel {
private(set) var titleObsrvable: Observable<String?>!
private(set) var colorObsrvable: Observable<UIColor?>!
init(from goal: Goal) {
let goalPredicate = NSPredicate(format: "(SELF = %@)", self.goal.objectID)
self.goalObservable = CoreDataModel.shared.rx.fetchObject(predicate: goalPredicate)
self.titleObsrvable = goalObservable.map { $0?.name }
self.colorObsrvable = goalObservable.map { $0?.color }
}
}
extension Reactive where Base: CoreDataModel {
func fetchObjects<T: NSManagedObject>(predicate: NSPredicate? = nil) -> Observable<[T]> {
let entityName = String(describing: T.self)
let fetchRequest = NSFetchRequest<T>(entityName: entityName)
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
fetchRequest.predicate = predicate
return base.managedObjectContext.rx.entities(fetchRequest: fetchRequest)
}
}
类Goal
只是类Entitiy
observeOn(MainScheduler.sharedInstance)
没有帮助❌替换(及其作品✅为什么):
self.titleObsrvable = goalObservable.map { $0?.name }
self.colorObsrvable = goalObservable.map { $0?.color }
使用:
self.titleObsrvable = Observable.of("1234")
self.colorObsrvable = Observable.of(UIColor.red)
知道我做错了吗?
如果您想查看整个项目,可以在此处查看: https://github.com/Yerkenabildin/my-everest
答案 0 :(得分:0)
这似乎是某种竞争条件。您正在导航控制器开始动画的同一时刻设置barTintColor
。如果您确定在动画完成之后 之前不改变锡颜色,它将按照您想要的方式工作。
此外,您有两个不同的绑定器到相同导航栏的rx.barTintColor,他们正在提供条冲突信息。如果可能的话,你应该只有一个活页夹。