Swift 2,无法识别的选择器发送到 UIAlertController 的实例
Posted
技术标签:
【中文标题】Swift 2,无法识别的选择器发送到 UIAlertController 的实例【英文标题】:Swift 2, unrecognized selector sent to instance for UIAlertController 【发布时间】:2016-06-03 06:15:21 【问题描述】:点击搜索后点击 TableView 时出现如下错误。
以下错误 2016-06-03 11:20:31.578 ContactPLUS[1724:678512] 警告:尝试在 已经呈现(空) 圣>> 2016-06-03 11:20:32.001 ContactPLUS[1724:678512]-[_UIAlertControllerAlertPresentationController AdaptivePresentationController]: 无法识别的选择器发送到实例 0x1616570d0
2016-06-03 11:20:32.001 ContactPLUS[1724:678512] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序, 原因:'-[_UIAlertControllerAlertPresentationController AdaptivePresentationController]:无法识别的选择器发送到实例 0x1616570d0'
*** 首先抛出调用堆栈: (0x182876e38 0x181edbf80 0x18287dccc 0x18287ac74 0x182778d1c 0x18836433c 0x1880117f8 0x187d20740 0x187c72fd8 0x187c80990 0x1879b24a4 0x18282c7b0 0x18282a554 0x18282a984 0x182754d10 0x18403c088 0x187a29f70 0x1000f8070 0x1822f28b8) libc++abi.dylib:以 NSException 类型的未捕获异常终止
下面是我的代码
进入函数
dispatch_async(dispatch_get_main_queue())
self.searchActivityLoader.stopAnimating()
self.searchActivityLoader.hidden = true
self.showError(self.noDataFound)
显示警报控制器的逻辑:
func showError(errorStr: String)
dispatch_async(dispatch_get_main_queue(),
if(!errorStr.isEmpty)
let alertController = self.controllerUtil!.customOkAlert("Fail",buttonTitle:"OK" , alertMessage: errorStr)
self.presentViewController(alertController, animated: true, completion: nil)
//**Error comes after executing above two lines**
self.genericArr = []
self.contactsTableView.dataSource = self
self.contactsTableView.reloadData()
self.searchActivityLoader.stopAnimating()
self.searchActivityLoader.hidden = true
)
///CustomeOkAlert
//警报控制器使用默认操作和自定义消息进行管理
func customOkAlert(alertPageTitle: String,buttonTitle: String, alertMessage: String) -> UIAlertController
let customeAlert = UIAlertController(title: alertPageTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.Alert)
customeAlert.view.layer.cornerRadius = 10
customeAlert.view.backgroundColor = utility.uicolorFromHex(0xEBEBED)
customeAlert.view.tintColor = utility.uicolorFromHex(0x70B420)
let cancelAction: UIAlertAction = UIAlertAction(title: buttonTitle, style: .Default)action in
//do nothing
customeAlert.addAction(cancelAction)
return customeAlert
【问题讨论】:
你能发布 customOkAlert() 方法吗? 你能在你的showError
函数中加入一个print("someText")
,看看它是否被调用了两次吗?
【参考方案1】:
Attempt to present <UIAlertController which is already presenting
的原因是经常报错,需要添加检查是否已经出现报错信息。
以下代码帮助我重现了您的问题。在handleError
方法中评论检查将开始问题再现:
class ViewController: UIViewController
var alertController: UIAlertController!
override func viewDidLoad()
super.viewDidLoad()
handleError()
handleError()
func handleError()
dispatch_async(dispatch_get_main_queue(),
//commenting this check will start issue reproduction
if self.alertController != nil
print("alert is already presented")
return
self.alertController = self.customOkAlert("Fail",buttonTitle:"OK" , alertMessage: "qweqwe")
self.presentViewController(self.alertController, animated: true, completion: nil)
)
func customOkAlert(alertPageTitle: String,buttonTitle: String, alertMessage: String) -> UIAlertController
let customeAlert = UIAlertController(title: alertPageTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.Alert)
customeAlert.view.layer.cornerRadius = 10
let cancelAction: UIAlertAction = UIAlertAction(title: buttonTitle, style: .Default)action in
self.alertController = nil
customeAlert.addAction(cancelAction)
return customeAlert
【讨论】:
B,尝试过相同的..但似乎我遇到了同样的错误。以上是关于Swift 2,无法识别的选择器发送到 UIAlertController 的实例的主要内容,如果未能解决你的问题,请参考以下文章