我在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