【发布时间】:2021-10-11 19:18:39
【问题描述】:
我有一个 MasterViewController,它是一个 UIPageViewController,它有四个视图控制器。任务数组包含任务对象。这个任务数组应该只有一个实例,并且应该在孩子之间共享,因此所做的任何更改都会在相应的视图控制器中更新(我将使用 NotificationCenter 通知视图控制器)
class MasterViewController: UIPageViewController {
let vcs = [TodayListViewController(), PlannerViewController(), ReviewListViewController(), AddViewController()]
static var tasks = [
Task(id: "1", title: "Mock up onboarding", completed: true, priority: 0),
Task(id: "2", title: "Connect to Firebase", completed: false, priority: 1),
Task(id: "3", title: "Code review", completed: false, priority: 0),
Task(id: "4", title: "Add beta testers to TestFlight", completed: false, priority: 1)
]
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor(named: "Gray")
dataSource = self
setViewControllers([vcs[0]], direction: .forward, animated: false)
}
}
我目前的解决方案
我已将数组设为静态,因此只需键入即可在子视图控制器中访问它
MasterViewController.tasks
MasterViewController.tasks.append(task) // When adding a new task in the AddViewController
这是一种可接受的方法吗?
单例解决方案
我尝试过制作一个单例并将数组存储在那里,它确实有效,但是由于围绕它的争议,我不喜欢使用单例设计模式的想法。
【问题讨论】:
标签: ios swift uipageviewcontroller