gpt4 book ai didi

ios - 使用主题获得响应后将数据从父 VC 传递到子 VC

转载 作者:行者123 更新时间:2023-11-28 05:44:45 29 4
gpt4 key购买 nike

我有一个 VC(A),它有一个容器 View 并根据 segementControl 值更改它的 VC(B-C),我在 A ViewController 中发送请求并获得响应,我想确保 B 和C View Controller 观察A的响应并设置数据

我是 rxswift 的新手,所以请多多包涵

在一个 VC 中启动两个子 VCS

private lazy var profileVC: ProfileVC = {
// Load Storyboard
let storyboard = UIStoryboard(name: "Profile", bundle: Bundle.main)

// Instantiate View Controller
var viewController = storyboard.instantiateViewController(withIdentifier: "ProfileVC") as! ProfileVC

// Add View Controller as Child View Controller
self.add(asChildViewController: viewController)

return viewController
}()

private lazy var socialMediaVC: SocialMediaVCViewController = {
// Load Storyboard
let storyboard = UIStoryboard(name: "Profile", bundle: Bundle.main)

// Instantiate View Controller
var viewController = storyboard.instantiateViewController(withIdentifier: "SocialMediaVC") as! SocialMediaVCViewController

// Add View Controller as Child View Controller
self.add(asChildViewController: viewController)

return viewController
}()

在 ViewController 中添加和删除函数

func add(asChildViewController viewController: UIViewController) {
// Add Child View as Subview
containerView.addSubview(viewController.view)

// Configure Child View
viewController.view.frame = view.bounds
viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}



private func remove(asChildViewController viewController: UIViewController) {
// Notify Child View Controller
viewController.willMove(toParent: nil)

// Remove Child View From Superview
viewController.view.removeFromSuperview()

// Notify Child View Controller
viewController.removeFromParent()
}

segemntController 改变

 @IBAction func segmentedControlClicked(_ sender: UISegmentedControl) {
segmentedControl.changeUnderlinePosition()
if segmentedControl.selectedSegmentIndex == 0 {
remove(asChildViewController: socialMediaVC)
add(asChildViewController: profileVC)
} else {
remove(asChildViewController: profileVC)
add(asChildViewController: socialMediaVC)
}
}

在 ViewModel 中发送请求和获取响应:

    startedUp.accept(true)
startedUp.filter({ $0})
.flatMap{ [weak self] _ -> Observable<Event<Result<ProfileResponse>>> in
self?.loadInProgress.accept(true)
return network.showProfile(startupId: 1).materialize()}
.subscribe(onNext: { [weak self] event in
self?.loadInProgress.accept(false)
switch event {
case .next(let result):
switch result{
case .Success(let response):
self?.startedUp.accept(false)
self?.sectionSubject.onNext(0)
self?.output.editProfileData = (self?.setProfileData(response:response))!
self?.tableViewcellsSubject.onNext((self?.createArray(response: response))!)
self?.userscellsSubject.onNext((self?.createUserArray(response: response.users!))!)
case .Failure(let error):
self?.errorsSubject.onNext(error)
}
case .error( _):
print("error")
default:
break
}
})
.disposed(by: disposeBag)

最佳答案

假设您的 AViewModel 有您的 child 感兴趣的数据流:

protocol AViewModel {
let importantDataStream: Observable<SomeData> { get }
}

并且您的 ParentVC 在他的 View 模型上有如下引用:

var viewModel: AViewModel

然后你可以声明协议(protocol):

protocol ImportantDataListener {
func subscribe(to stream: Observable<SomeData>)
}

B和C都会遵守这个协议(protocol)。

然后我们像这样更改 add(asChildViewController ...) 函数的声明:

func add(asChildViewController viewController: UIViewController & ImportantDataListener) {
// Add Child View as Subview
containerView.addSubview(viewController.view)

// Configure Child View
viewController.view.frame = view.bounds
viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

// Passing stream
viewController.subscribe(to: viewModel.importantDataStream)
}

关于ios - 使用主题获得响应后将数据从父 VC 传递到子 VC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55237803/

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