问题
I am trying to call a UIAlertController
from within my UITtableViewCell
when my function is called. It gives me an error saying present is not available. I understand it's not within a ViewController
. I am looking for an approach to access it.
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)
}
回答1:
You can try to use delegate
protocol AlertShower{
func showAlert(TableCustomCell)
}
class TableCustomCell: UITableViewCell {
var delegate: AlertShower?
@IBAction func showClicked(_ sender: UIButton) {
self.delegate?.alertShower(sender:self)
}
}
in the VC
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
}
}
回答2:
Present is only available to ViewControllers. You are going to have to redirect the touch event to your view controller. The most common way of doing this would be having a delegate property in your UITableViewCell.
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID276
回答3:
It's not working for me. Here is my code:
import UIKit
protocol AlertShower{
func showAlert(_: allListsCell)
}
// prototype table cell...
class allListsCell: UITableViewCell {
var delegate: AlertShower?
@IBOutlet var cellSelected: UILabel!
var colorIndex = Int() // point directly back to colors[]
@IBAction func colorEditButton(_ sender: UIButton, forEvent event: UIEvent) {
self.delegate?.alertShower(sender:self) // ERROR...
}
}
The error above is from the compiler:
"Value of type 'AlertShower' has no member 'alertShower'"
The rest of the code:
class tableViews: UITableViewController, AlertShower {
func showAlert(_: allListsCell) {
}
func showAlert(sender:allListsCell) {
// show alert here
}
...
}
...help?
回答4:
I ran into a similar problem myself when creating a custom activity indicator from a subclassed UIView
. What I did was create the 'show' function (in the subclass) and pass in a UIViewController
parameter, like so:
public func play(inView view: UIViewController) {
//Perform action here
view.present(alertController, animated: true, completion: nil)
}
Simply call it in your view controller like so:
CustomClass.play(inView: self)
Hopefully this helps!
来源:https://stackoverflow.com/questions/49199716/calling-uialertcontroller-inside-a-uitableviewcell