从UITableViewCell呈现UIAlertController

编程入门 行业动态 更新时间:2024-10-28 19:33:20
本文介绍了从UITableViewCell呈现UIAlertController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将警报添加到自定义 UITableViewCell 以显示 UIAlertView 我需要从的UIViewController 。但是,我不知道如何从 UITableViewCell 类访问当前的 UIViewController 实例。以下代码是我尝试使用扩展程序执行此操作。

I am trying to add alerts to a custom UITableViewCell to present an UIAlertView I need to call presentViewController from UIViewController. However, I am unaware of how to gain access to the current UIViewController instance from the UITableViewCell class. The below code is my attempt to do this with an extension.

我收到此错误

表达式已解析为未使用的函数。

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 {

推荐答案

您可以使用此扩展程序查找显示单元格的viewController

You can use this extension to find the viewController that present the cell

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)

更多推荐

从UITableViewCell呈现UIAlertController

本文发布于:2023-11-27 19:28:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639304.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UITableViewCell   UIAlertController

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!