I am trying to open a second UIAlertController upon choosing an option in the first UIAlertController but I am unable to reach indexPath in order to set a property from a textField of a second AlertController. Here’s the snippet:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let ac = UIAlertController(title: "Choose an action", message: nil, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Rename a person", style: .default, handler: alert2))
ac.addAction(UIAlertAction(title: "Delete", style: .destructive) {
[weak self] _ in
self?.people.remove(at: indexPath.item)
self?.collectionView.reloadData()
})
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
present(ac, animated: true)
}
func alert2(alert: UIAlertAction!) {
let ac2 = UIAlertController(title: "Type in a name", message: nil, preferredStyle: .alert)
ac2.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
self.present(ac2, animated: true)
guard let newName = ac2.textFields?[0].text else { return }
let person = people[indexPath.item]
person.name = newName
self.collectionView.reloadData()
ac2.addTextField()
}
I also get the "Cannot find ‘indexPath’ in scope" error in this string:
let person = people[indexPath.item]
Could you please help me to work it out?
2
Answers
You need to pass the indexPath into the
alert2
function. Try this newalert2
function:And you would replace this line:
with this:
Cannot find ‘indexPath’ in scope is given because your function is not able to find index number. you should pass Indexpath parameter through your function alert2
change your function
func alert2(alert: UIAlertAction!)
tofunc alert2(alert: UIAlertAction!, indexPath:IndexPath)
then you will found the index.for your reference :