【发布时间】:2018-03-09 18:08:19
【问题描述】:
当我的函数被调用时,我试图从我的UITtableViewCell 中调用UIAlertController。它给了我一个错误,说礼物不可用。我知道它不在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)
}
【问题讨论】:
-
理想情况下,不要从单元格中显示视图控制器。相反,创建一个委托或回调函数并将事件传递给视图控制器并从那里显示警报。
标签: ios swift uitableview uialertcontroller