我正在尝试在调用我的函数时从UIAlertController
内调用UITtableViewCell
。它给我一个错误,说现在不可用。我知道它不在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)
}
答案 0 :(得分:2)
您可以尝试使用委托
protocol AlertShower{
func showAlert(TableCustomCell)
}
class TableCustomCell: UITableViewCell {
var delegate: AlertShower?
@IBAction func showClicked(_ sender: UIButton) {
self.delegate?.alertShower(sender:self)
}
}
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
}
}
答案 1 :(得分:1)
Present仅适用于ViewControllers。您将不得不将触摸事件重定向到视图控制器。最常见的方法是在UITableViewCell中使用委托属性。
答案 2 :(得分:0)
它不适合我。这是我的代码:
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...
}
}
上述错误来自编译器:
"类型的值' AlertShower'没有会员' alertShower'"
其余代码:
class tableViews: UITableViewController, AlertShower {
func showAlert(_: allListsCell) {
}
func showAlert(sender:allListsCell) {
// show alert here
}
...
}
...帮助?
答案 3 :(得分:-2)
我在从子类UIView
创建自定义活动指标时遇到了类似的问题。我所做的是创建'show'函数(在子类中)并传入UIViewController
参数,如下所示:
public func play(inView view: UIViewController) {
//Perform action here
view.present(alertController, animated: true, completion: nil)
}
只需在视图控制器中调用它,就像这样:
CustomClass.play(inView: self)
希望这有帮助!