我在listViewCell.swift和listViewCell.xib中设计了自定义单元格。其中我有textfield。当用户输入超过100的数字时,我想显示警报。 我在HomeViewController中有tableView。在swift中显示自定义单元格的警报3
let alert = UIAlertController(title: "Error", message: "Please Insert value less than 100", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
然后它给了我Attempt to present <UIAlertController: 0x7f945a5a67d0> on <ProjName.LoginController: 0x7f945a407e70> whose view is not in the window hierarchy!
,当我更改代码以
let alert = UIAlertController(title: "Error", message: "Please Insert value less than 100", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
HomeViewController().present(alert, animated: true, completion: nil)
然后它给了我unexpectedly found nil while unwrapping an Optional value
并指向 `覆盖FUNC viewDidLoad中(){ 超。 viewDidLoad()
**tableViewList.delegate = self**
tableViewList.dataSource = self
searchBar.delegate = self`
如何显示来自自定义单元格的警报?或者我们如何在HomeViewController中创建一个通用的提醒函数并将其显示为任何swift文件?
在此先感谢
你在哪一行得到这个错误? – KKRocks
你的tableview和alert是否在同一个HomeController上?如果是这样而不是HomeViewController(),为什么不尝试自我。 (alert,animated:true,completion:nil) – Janmenjaya
我在'tableViewList.delegate = self'处得到错误 – Kalpesh