0
I am studying Swift and at the moment I am trying to make an application with more than one screen, very simple.
But I’m having trouble working with these inter-screen, storyboard connections.
I identified one of the components of one of the screens as ToDoItem
, image below:
I’m trying to click the line and carry the value to another screen, but I can’t do the cast
for the identifier ToDoItem
, Xcode always returns an error to me:
Follow the code of Todolist:
class ToDoListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var toDoItems: [ToDoItemModel] = [ToDoItemModel]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView()
title = "To Do List"
let testItem = ToDoItemModel(name: "Test Item", details: "TestDetails", completionDate: Date())
self.toDoItems.append(testItem)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedItem = toDoItems[indexPath.row]
performSegue(withIdentifier: "TaskDetailsSegue", sender: selectedItem ) // -> passa dados para outra tela
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return toDoItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let toDoItem = toDoItems[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoItem")!
cell.textLabel?.text = toDoItem.isComplete ? "Complete" : "Incomplete"
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "TaskDetailsSegue"{
guard let destinationVC = segue.destination as? ToDoDetailsViewController else { return }
guard let toDoItem = sender as? ToDoItem else { return } //erro
destinationVC.toDOItem = toDoItem
}
}
}
The mistake Use of undeclared type 'ToDoItem'
appears on the Guard line let toDoItem = sender as? ToDoItem else { return }
What should I do to get this information and take it to another screen of this storyboard?
I got it, thank you very much
– Henrique