In chapter 18, where we add the code to perform a segue to the last opened list before the app was terminated, I wanted to try to go a little further and figure out if I could have the app perform a segue to the “ShowChecklist” screen right after I add a new checklist. I figure this would be best handled in AllListsViewController.swift, since it is already performing such segues if the user has the checklist selected and quits the app.
I added some lines to the ListDetailVC(_:didFinishAdding:) delegate function in AllListsVC that handles adding a new list:
func listDetailViewController(_ controller: ListDetailViewController, didFinishAdding checklist: Checklist) {
let newRowIndex = dataModel.lists.count
dataModel.lists.append(checklist)
let indexPath = IndexPath(row: newRowIndex, section: 0)
let indexPaths = [indexPath]
tableView.insertRows(at: indexPaths, with: .automatic)
navigationController?.popViewController(animated: true)
dataModel.indexOfSelectedChecklist = newRowIndex
performSegue(withIdentifier: "ShowChecklist", sender: checklist)
}
The last two lines are all I added, after the nav controller pops the item detail screen and goes back to the All Checklists screen, I set the dataModel index to the new row index, and then perform the segue to the newly added checklist. This segue itself works, however, if I go to add an item to the new checklist, I get a debugger output “Unknown class AddItem in Interface Builder file”. The item is saved though, so I’m not sure what I am supposed to do about that either.
Print statements I added tell me that the pop back to the main screen resets the dataModel.indexOfSelectedChecklist back to -1 AFTER this function sets it to the new row index, and the -1 is carried into the AppDelegate saveData function, and the newly added list is not returned to after quitting and restarting the app. This functionality works only if I go back and tap the list, like it should.
I tried to comment out the call to popViewController to see what would happen, which succeeded in not resetting the dataModel value back to -1, but instead wrecked control flow for the Nav Controller, letting me press the back button to go from the individual checklist view back to the checklist add screen. If I hit Cancel then, the screen goes back to the main Checklists view, while still adding the new list; and If I were to hit Done instead of Cancel, it just repeats and won’t let me go back to the main screen until I hit back, then Cancel, at which point it would have added as many duplicate lists as the amount of times I hit Done.
So to sum it up, Im trying to figure out the proper way to segue automatically into a newly created list, while also retaining the ability to save that index and return to the new list if the app is quit while the new list is selected. I guess I am not really sure how all this fits together still, and any insight into how to go about doing that, or where I went wrong, would be greatly appreciated.
Thanks!
Ryan