Search code examples
iosswiftuitableviewuiactionsheet

Fill action sheet dynamically


I'm looking for a way to make a floating list (TableView inside AlertView) with data from a list of objects and 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 btnShowList_onClick(_ sender: Any) {
    let alertController = UIAlertController(title: "Action Sheet", message: "List of Superheroes", preferredStyle: .actionSheet)

    for item in arrSuperHeroes{
        let superbutton = UIAlertAction(title: (item as! SuperHeroe).nombre , style: .default, handler: { (action) in
            self.superSel = item as! SuperHeroe
            print(self.superSel.name)
        })
        alertController.addAction(superbutton)
    }

    self.navigationController!.present(alertController, animated: true, completion: nil)
}

But I have this error in the execution of the last line

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

Can anybody help me? What's wrong? Is there any other way to do this?


Solution

  • I think you need to present the alert as follow:

    self.present(alertController, animated: true, completion: nil)