gpt4 book ai didi

ios - 在 UITableViewCell 中调用 UIAlertController

转载 作者:行者123 更新时间:2023-11-28 14:57:22 25 4
gpt4 key购买 nike

当我的函数被调用时,我试图从我的 UITtableViewCell 中调用一个 UIAlertController。它给我一个错误,说 present is not available。我知道它不在 ViewController 中。我正在寻找一种访问它的方法。

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code

let tapGestureShareImageView = UITapGestureRecognizer(target: self, action: #selector(self.shareImageTouchUpInside))
shareImageView.addGestureRecognizer(tapGestureShareImageView)
shareImageView.isUserInteractionEnabled = true
}

@objc func shareImageTouchUpInside() {
showAction()
}

func showAction() {
let alertController = UIAlertController(title: "Action Sheet", message: "What do you like to do", preferredStyle: .alert)

let okButton = UIAlertAction(title: "Done", style: .default, handler: { (action) -> Void in
print("Ok button tapped")
})

let deleteButton = UIAlertAction(title: "Skip", style: .destructive, handler: { (action) -> Void in
print("Delete button tapped")
})

alertController.addAction(okButton)
alertController.addAction(deleteButton)

present(alertController, animated: true, completion: nil)
}

最佳答案

你可以尝试使用delegate

protocol AlertShower{
func showAlert(TableCustomCell)
}

class TableCustomCell: UITableViewCell {
var delegate: AlertShower?

@IBAction func showClicked(_ sender: UIButton) {
self.delegate?.alertShower(sender:self)
}
}

在风投中

class viewController: UIViewController, AlertShower {

override func viewDidLoad() {
super.viewDidLoad()

}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = areaSettTable.dequeueReusableCell(withIdentifier:CellIdentifier1) as! TableCustomCell

cell.delegate = self

return cell
}

func showAlert(sender:TableCustomCell) {

// show alert here

}
}

关于ios - 在 UITableViewCell 中调用 UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49199716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com