Ios UITableView中的UIAlertController返回零
我正在UITableView中使用UIAlertController。它返回tableview.delegate=self附近的nil值。这里alertC是表示警报的函数。当按下按钮时,我从tableViewCell调用alertC函数Ios UITableView中的UIAlertController返回零,ios,swift,uitableview,uialertcontroller,tableviewcell,Ios,Swift,Uitableview,Uialertcontroller,Tableviewcell,我正在UITableView中使用UIAlertController。它返回tableview.delegate=self附近的nil值。这里alertC是表示警报的函数。当按下按钮时,我从tableViewCell调用alertC函数 class AdminPlaces: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! priva
class AdminPlaces: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
private var placeNames = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.getDataFromFirebase {
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.reloadData()
}
func alertC() {
let ac = UIAlertController(title: "Delete", message: "Are you sure to delete the place", preferredStyle: .alert)
let yesAction = UIAlertAction(title: "Yes", style: .default, handler: nil)
let noAction = UIAlertAction(title: "No", style: .cancel, handler: nil)
ac.addAction(yesAction)
ac.addAction(noAction)
self.present(ac, animated: true, completion: nil)
}
}
class AdminPlacesCell: UITableViewCell {
@IBOutlet weak var placeName: UILabel!
@IBOutlet weak var delete: UIButton!
@IBOutlet weak var edit: UIButton!
override func awakeFromNib() {
edit.alpha = 0
delete.alpha = 0
}
func configure(place: String) {
placeName.text = place
}
func showButtons() {
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseIn, animations: {
self.edit.alpha = 1
self.delete.alpha = 1
}, completion: nil)
}
@IBAction func onEditClicked(_ sender: Any) {
}
@IBAction func onDeleteClicked(_ sender: Any) {
let adminPlaces = AdminPlaces()
adminPlaces.alertC()
}
}
我不明白是什么问题。为什么tableView.delegate=self附近返回nil?正确的做法是什么
提前谢谢这里有几个问题。1.alertC存在于ViewDidLoad中。将此函数移出此处。2.以下代码:
let adminPlaces = AdminPlaces()
adminPlaces.alertC()
说创建一个全新的AdminPlaces实例,并在新实例上调用alertC()。这与在单元格中保存tableView的AdminPlaces不同。新警报尚未显示,因此不会显示警报。3.您的警报存在于ViewController上,而您的单元格没有直接访问它的方式
使用UITableViewDelegate方法tableView:commit:editingStyle。当用户从编辑模式删除单元格时,调用此方法。从这里调用您的alertC()方法 谢谢你的回复。但只是我没有在viewDidLoad中编写alertC()函数。这只是我复制粘贴的一个错误。谢谢。我从这个链接得到了答案。任何有同样问题的人都可以参考这个。请检查此链接,因为它解决了我的问题。