I am looking for a way to make a floating list (TableView inside AlertView) with data from a list of objects and that when clicking on an element gives me the reference to the selected object.
For this I am using an action sheet and a for loop as follows
@IBAction func btnVerLista_onClick(_ sender: Any) {
let alertController = UIAlertController(title: "Action Sheet", message: "Lista de Superheroes", preferredStyle: .actionSheet)
for item in arrSuperHeroes{
let superbutton = UIAlertAction(title: (item as! SuperHeroe).nombre , style: .default, handler: { (action) -> Void in
self.superSel = item as! SuperHeroe
print(self.superSel.nombre)
})
alertController.addAction(superbutton)
}
self.navigationController!.present(alertController, animated: true, completion: nil)
}
But I always have this error when executing the last line
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
Could someone help me with the idea of the error or if there is another way to do what I need.
The last line of the code looks like this:
More context would be missing but it gives the impression that
navigationController
it is indeednil
why the error occurs. However, it is not necessary to usenavigationController
to invoke the methodpresent
, in this particular case, you can do it overself
.The complete code would look like this:
The app looks like this: