【发布时间】:2017-05-31 08:15:28
【问题描述】:
我在listViewCell.swift 和listViewCell.xib 中设计了自定义单元格。我在其中有文本字段。当用户输入超过 100 的数字时,我想显示警报。
我在 HomeViewController 中有 tableView。
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
并指向
override func viewDidLoad() {
super.viewDidLoad()
**tableViewList.delegate = self**
tableViewList.dataSource = self
searchBar.delegate = self
}
如何显示来自自定义单元格的警报?或者我们如何在 HomeViewController 中创建一个通用的警报函数并将其显示在任何 swift 文件中?
提前致谢
【问题讨论】:
-
您在哪一行收到此错误?
-
你的 tableview 和 alert 是否在同一个 HomeController 上?如果是这样而不是 HomeViewController(),你为什么不试试 self.存在(警报,动画:真,完成:无)
-
tableViewList.delegate = self出现错误