I am trying to show an alert after another one in swift. I found a solution but I think this is not the true way. My code piece is below.
With this code, alerts can be shown in the view.
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let person = people[indexPath.item]
let questionAc = UIAlertController(title: "What is your purpose?", message: "", preferredStyle: .alert)
let deleteButton = UIAlertAction(title: "Delete", style: UIAlertAction.Style.destructive) { [weak self] _ in
self?.people.remove(at: indexPath.item)
collectionView.reloadData()
}
let renameButton = UIAlertAction(title: "Rename", style: .default) { [weak self] _ in
let ac = UIAlertController(title: "Rename person", message: nil, preferredStyle: .alert)
ac.addTextField()
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
ac.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak self, weak ac] _ in
guard let newName = ac?.textFields?[0].text else {return}
person.name = newName
self?.collectionView.reloadData()
}))
self?.present(ac, animated: true)
}
questionAc.addAction(deleteButton)
questionAc.addAction(renameButton)
present(questionAc, animated: true)
}
As you know, if I directly add my alert code after the first one, it doesn't work. But if I put my second one's code to first one's action, it does.
However, in this case, I need to connect one alert to another one's button's action. I don't want to connect it to another one.
Is there any solution for better way showing alerts in the same view, orderly?
Thank you.