Hi, I’m new to coding, and am attempting to build a to-do list app. I have created a table view, and in the cell, there is an accessory button. In the view controller, I wrote:
‘’’
func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
let mdvc = storyboard?.instantiateViewController(identifier: “MoreDetailController”) as? MoreDetailController
print(mdvc!.titleLabel?.text) //prints nil
mdvc?.titleLabel?.text = tasks[indexPath.row].name
print(tasks[indexPath.row].name) //prints correct name
print(mdvc!.titleLabel?.text) //prints nil
self.navigationController?.pushViewController(mdvc!, animated: true)
}
‘’’
In this code, it changes the view controller (this is successful when I run the app), yet the value of mdvc!.titleLabel?.text stays nil, even though tasks[indexPath.row].name is not nil (I printed this to make sure). To be clear, when this code runs, the page changes, the label on the new page does not. The code prints: nil, “Name of Task”, nil. For more clarification: in the new view controller called MoreDetailController,
‘’’
@IBOutlet weak var titleLabel: UILabel!
‘’’
I think the issue is to do with optional chaining but I can’t get it right, can someone please help me get the label text to become the tasks[indexPath.row].name? Thanks!