【发布时间】:2022-01-23 21:37:57
【问题描述】:
背景
我有一个 workoutVM 视图模型,它可以容纳我的大部分视图模型,我想将其传递给我的应用程序中的所有其他视图控制器。我的应用还有一个tabbar控制器,我用它来存储一些数据,比如用户信息等。
问题 1
即使我在MyTrainingViewController(“主页”选项卡)中创建了workoutVM 视图模型(带有值),我也无法将视图模型(带有值)传递给下一个ExerciseSetsViewController("锻炼”选项卡)使用
- 方法 1 - 通过使用委托
MyTrainingViewControllerDelegate和/或 - 方法 2 - 通过实例化
ExerciseSetsViewController并加载视图。
当用户选择特定的表格视图单元格时,将运行传递workoutVM 视图模型的代码/操作。
我真的不确定为什么其中任何一种方法都有效,因为类似的方法适用于许多其他场景。下面是 Xcode 调试器,显示我使用这两种方法的代码没有将锻炼虚拟机视图模型传递给 ExerciseSetsViewController
问题 2
结果,我找到了一个解决方法(方法 3 在下面的代码中被注释掉了) 来利用 tabbar 来存储 workoutVM 视图模型(再次依靠 tabbar 来传递和分享跨多个视图控制器的数据)。
在这一点上,我担心我的应用实际上是在将 tabbar 用作“单例”,尽管我“有点”理解它不是完全“单例”。
我认为,理想情况下,视图模型应该充当某种数据模型,它们可以在多个视图控制器之间共享/操作/传递,而无需将选项卡栏作为中间层。 这不是正确的吗?或者这是我通过使用标签栏采用的最佳/良好做法?
protocol MyTrainingViewControllerDelegate {
func passWorkoutVM(workoutVM: WorkoutViewModel)
}
class MyTrainingViewController: UIViewController {
var workoutVM: WorkoutViewModel?
var delegate: MyTrainingViewControllerDelegate!
@IBOutlet weak var dayProgramTableView: UITableView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
dayProgramTableView.delegate = self
dayProgramTableView.dataSource = self
}
}
extension MyTrainingViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let tabbar = tabBarController as! MainTabBarController
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let ExerciseSetsViewController = storyBoard.instantiateViewController(withIdentifier: "ExerciseSetsView") as! ExerciseSetsViewController
guard let workoutVM = self.workoutVM else {
return
}
print("printing workoutViewModel dayprogram no.of exercise at HomeView \(workoutVM.dayprograms[indexPath.row].dayprogram.dayIntensity)")
//1.Instantiate view via storyboard Method
ExerciseSetsViewController.loadViewIfNeeded()
ExerciseSetsViewController.workoutVM = workoutVM
//2.Delegate Method
self.delegate?.passWorkoutVM(workoutVM: workoutVM)
//3.Tabbar Method
// tabbar.workoutVM = workoutVM
tabbar.selectedIndex = 1
}
}
class ExerciseSetsViewController: UIViewController {
var workoutVM: WorkoutViewModel?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
// ** To do
//create workoutViewModel
//3.Tabbar Method
// self.workoutVM = tabbar.workoutVM
print("printing workoutViewModel to check if workoutVM had been passed from MyTrainingView \(String(describing: self.workoutVM))")
}
}
extension ExerciseSetsViewController: MyTrainingViewControllerDelegate {
func passWorkoutVM(workoutVM: WorkoutViewModel) {
self.workoutVM = workoutVM
print("passWorkoutDelegate Method executed")
}
}
class MainTabBarController: UITabBarController {
var workoutVM: WorkoutViewModel?
override func viewDidLoad() {
super.viewDidLoad()
}
}
【问题讨论】:
标签: ios swift mvvm delegates uitabbarcontroller