繁体   English   中英

通过UITableViewCell上的按钮显示UIAlertController

[英]Show an UIAlertController from a button on an UITableViewCell

这就是我在UITableViewCell的按钮内编写的代码。

var shareAlert = UIAlertController(title: "Post Alert", 
    message: "Your Post has been Shared with your Friends", 
    preferredStyle: UIAlertControllerStyle.Alert)

var Ok = UIAlertAction(title: "Ok", 
    style: UIAlertActionStyle.Default, 
    handler: nil)

shareAlert.addAction(Ok)
shareAlert.presentViewController(shareAlert, animated: true, completion: nil)

当用户单击按钮时,我希望它显示此警报,但是一旦单击按钮,应用程序就会崩溃。 我做错了吗? 如何从UITableViewCell的按钮显示警报视图?

由于这条线而崩溃

shareAlert.presentViewController(shareAlert, animated: true, completion: nil)

控制器无法显示自己,因此

更换

shareAlert.presentViewController(shareAlert, ...) 

通过

yourCurrentViewController.presentViewController(shareAlert, ...)

而且您不应该使用UIAlertView,因为它已在iOS 9中弃用,并且在下一个iOS中可能已过时,因此请使用UIAlertController,并且您不必在不久的将来对其进行更改

请检查以下内容: https : //stackoverflow.com/a/32574681/5304286

您可以在此文件中调用alertShow函数,以通过以下方式显示简单消息:

 Utility.UI.alertShow("message", withTitle: "title", viewController: self)

除了使用UIAlertController还可以使用UIAlertView ,如下所示:

 var alert = UIAlertView(title: "Post Alert", message: "Your Post has been Shared with your Friends", delegate: nil, cancelButtonTitle: "Ok")
 alert.show()

文档: https : //developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html

注意:

UIAlertView在iOS 9中已弃用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM