I'm making a task-sharing app where you can share multiple lists of tasks with multiple individuals (like one with your spouse, one with your kid, etc.). Each list's data source is a struct called TaskList
, so I will have multiple instances of TaskList
. Where should I initialize those instances?
I'm new at this and any help would be greatly appreciated!
This is the code for the UITableViewController
that creates the task list using struct TaskList:
import UIKit
class LoLFirstTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 60.0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tasks.count
}
@IBAction func cancelToLoLFirstTableViewController(_ segue:UIStoryboardSegue) {
}
@IBAction func saveAddTask(_ segue:UIStoryboardSegue) {
if let AddTaskTableViewController = segue.source as? AddTaskTableViewController {
if let task = AddTaskTableViewController.task {
tasks.append(task)
let indexPath = IndexPath(row: tasks.count-1, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)
}
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! TaskCell
let task = tasks[indexPath.row]
cell.task = task
if cell.accessoryView == nil {
let cb = CheckButton()
cb.addTarget(self, action: #selector(buttonTapped(_:forEvent:)), for: .touchUpInside)
cell.accessoryView = cb
}
let cb = cell.accessoryView as! CheckButton
cb.check(tasks[indexPath.row].completed)
return cell
}
func buttonTapped(_ target:UIButton, forEvent event: UIEvent) {
guard let touch = event.allTouches?.first else { return }
let point = touch.location(in: self.tableView)
let indexPath = self.tableView.indexPathForRow(at: point)
var tappedItem = tasks[indexPath!.row] as Task
tappedItem.completed = !tappedItem.completed
tasks[indexPath!.row] = tappedItem
tableView.reloadRows(at: [indexPath!], with: UITableViewRowAnimation.none)
}
}
This is the code for TaskList:
import UIKit
struct TaskList {
var buddy: String
var phoneNumber: String
var tasks: [Task]
}
First option. You can create some kind of manager and store that list in that object:
class TaskListsManager
{
var taskLists:[TaskList] = []
}
And then pass instance of that object between your viewcontrollers:
let taskListsManager = TaskListsManager()
firstViewController.taskListsManager = taskListsManager
secondViewController.taskListsManager = taskListsManager
Second option would be to create a singleton object:
class TaskListsManager
{
static let sharedInstance = TaskListsManager()
var taskLists:[TaskList] = []
}
And you can use it anywhere in your app:
let first = TaskListsManager.sharedInstance.taskLists.first