从 UITableViewCell 呈现 UIAlertController
Presenting UIAlertController from UITableViewCell
我正在尝试向自定义 UITableViewCell
添加警报以显示 UIAlertView
我需要从 UIViewController
调用 presentViewController。但是,我不知道如何从 UITableViewCell
class 访问当前 UIViewController
实例。下面的代码是我尝试使用扩展来做到这一点。
我收到这个错误
Expression resolved to unused function.
extension UIViewController
{
class func alertReminden(timeInterval: Int)
{
var refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.Alert)
refreshAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
Alarm.createReminder("Catch the Bus",
timeInterval: NSDate(timeIntervalSinceNow: Double(timeInterval * 60)))
}))
refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
println("Handle Cancel Logic here")
}))
UIViewController.presentViewController(refreshAlert)
}
}
class CustomRouteViewCell: UITableViewCell {
尝试替换为这行代码:
Swift 2
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(refreshAlert, animated: true, completion: nil)
Swift 3
UIApplication.shared.keyWindow?.rootViewController?.present(refreshAlert, animated: true, completion: nil)
您可以使用此扩展来查找显示单元格的 viewController
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.nextResponder()
if parentResponder is UIViewController {
return parentResponder as! UIViewController!
}
}
return nil
}
}
或使用rootViewController
来呈现:
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(refreshAlert, animated: true, completion: nil)
Swift 4.2更新
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if parentResponder is UIViewController {
return parentResponder as? UIViewController
}
}
return nil
}
}
或使用rootViewController
来呈现:
UIApplication.shared.keyWindow?.rootViewController?.present(alertVC, animated: true, completion: nil)
另一种方法是在 table 视图单元格 class 中声明协议和委托 属性,将控制器设置为单元格委托并在中实现协议能够呈现警报视图控制器的控制器。
Leo 的解决方案对我有用。我用它来显示自定义集合视图单元格中的 AlertView。
Swift3.0 更新:
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if parentResponder is UIViewController {
return parentResponder as! UIViewController!
}
}
return nil
}
}
希望对您有所帮助。
您可以尝试使用协议和委托,使用此方法不仅可以提供警报,还可以让您更好地与视图控制器交互,以备日后需要时使用。例如:
protocol alerts: class{
func presentAlert(title:String, message:String)
}
然后在您的 tableViewCell 中创建如下内容:
var alertsDelegate : alerts?
//And whenever you need to present a message call something inside the cell like:
alertsDelegate?.presentAlert(title:"Your title", message:"Your message")
然后在您的 viewController 中,首先将您的 viewController 设置为警报
class yourClassName : ViewController, alerts {
//And add the functions of the protocol
func presentAlert(title:String, message:String){
//This is the function that'll be called from the cell
//Here you can personalize how the alert will be displayed
}
}
其次,在"cellForRowAt"方法中为单元格设置委托
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourIdentifier") as! yourCellClass
cell.alertsDelegate = self
}
就像这样,您可以创建多个功能来适应您与 viewController 或 viewController 通信的需要。
必须在 CustomTableViewCell Class and called these properties at the place of UIAlertViewController
中声明协议及其委托 属性,并在 UIViewController
中实现这些协议属性。
我正在尝试向自定义 UITableViewCell
添加警报以显示 UIAlertView
我需要从 UIViewController
调用 presentViewController。但是,我不知道如何从 UITableViewCell
class 访问当前 UIViewController
实例。下面的代码是我尝试使用扩展来做到这一点。
我收到这个错误
Expression resolved to unused function.
extension UIViewController
{
class func alertReminden(timeInterval: Int)
{
var refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.Alert)
refreshAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
Alarm.createReminder("Catch the Bus",
timeInterval: NSDate(timeIntervalSinceNow: Double(timeInterval * 60)))
}))
refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
println("Handle Cancel Logic here")
}))
UIViewController.presentViewController(refreshAlert)
}
}
class CustomRouteViewCell: UITableViewCell {
尝试替换为这行代码:
Swift 2
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(refreshAlert, animated: true, completion: nil)
Swift 3
UIApplication.shared.keyWindow?.rootViewController?.present(refreshAlert, animated: true, completion: nil)
您可以使用此扩展来查找显示单元格的 viewController
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.nextResponder()
if parentResponder is UIViewController {
return parentResponder as! UIViewController!
}
}
return nil
}
}
或使用rootViewController
来呈现:
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(refreshAlert, animated: true, completion: nil)
Swift 4.2更新
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if parentResponder is UIViewController {
return parentResponder as? UIViewController
}
}
return nil
}
}
或使用rootViewController
来呈现:
UIApplication.shared.keyWindow?.rootViewController?.present(alertVC, animated: true, completion: nil)
另一种方法是在 table 视图单元格 class 中声明协议和委托 属性,将控制器设置为单元格委托并在中实现协议能够呈现警报视图控制器的控制器。
Leo 的解决方案对我有用。我用它来显示自定义集合视图单元格中的 AlertView。
Swift3.0 更新:
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if parentResponder is UIViewController {
return parentResponder as! UIViewController!
}
}
return nil
}
}
希望对您有所帮助。
您可以尝试使用协议和委托,使用此方法不仅可以提供警报,还可以让您更好地与视图控制器交互,以备日后需要时使用。例如:
protocol alerts: class{
func presentAlert(title:String, message:String)
}
然后在您的 tableViewCell 中创建如下内容:
var alertsDelegate : alerts?
//And whenever you need to present a message call something inside the cell like:
alertsDelegate?.presentAlert(title:"Your title", message:"Your message")
然后在您的 viewController 中,首先将您的 viewController 设置为警报
class yourClassName : ViewController, alerts {
//And add the functions of the protocol
func presentAlert(title:String, message:String){
//This is the function that'll be called from the cell
//Here you can personalize how the alert will be displayed
}
}
其次,在"cellForRowAt"方法中为单元格设置委托
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourIdentifier") as! yourCellClass
cell.alertsDelegate = self
}
就像这样,您可以创建多个功能来适应您与 viewController 或 viewController 通信的需要。
必须在 CustomTableViewCell Class and called these properties at the place of UIAlertViewController
中声明协议及其委托 属性,并在 UIViewController
中实现这些协议属性。