我正在继承 AlertController 并收到错误“UIAlertController must have a title, a message or an action to display”
Posted
技术标签:
【中文标题】我正在继承 AlertController 并收到错误“UIAlertController must have a title, a message or an action to display”【英文标题】:I'm subclassing AlertController and get the error 'UIAlertController must have a title, a message or an action to display' 【发布时间】:2020-05-12 16:33:18 【问题描述】:final class AlertViewComponent: UIAlertController
private(set) var model: Model!
private let alert: UIAlertController
init()
self.alert = UIAlertController(title: "abc", message: "def", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: L10n.Generic.Label.ok, style: .default, handler: nil))
super.init(nibName: nil, bundle: nil)
required init?(coder: NSCoder)
fatalError("init(coder:) has not been implemented")
func configureAlert(options: AlertOption)
self.alert.title = self.model.title
self.alert.message = self.model.message
switch options
case let .oneOption(handler):
alert.addAction(UIAlertAction(title: L10n.Generic.Label.ok, style: .default, handler: handler))
case let .twoOptions(handlerYes, handlerNo):
alert.addAction(UIAlertAction(title: L10n.Generic.Label.ok, style: .default, handler: handlerYes))
alert.addAction(UIAlertAction(title: L10n.Generic.Label.yes, style: .default, handler: handlerNo))
extension AlertViewComponent: Component
enum AlertOption
case oneOption(handler: (UIAlertAction) -> Void)
case twoOptions(handlerYes: (UIAlertAction) -> Void, handlerNo: (UIAlertAction) -> Void)
struct Model
let title: String
let message: String
init(title: String = "", message: String)
self.title = title
self.message = message
enum Configuration
case update(model: Model)
func render(with configuration: AlertViewComponent.Configuration)
switch configuration
case let .update(model):
self.model = model
我使用以下命令初始化警报:
private let alertViewComponent: AlertViewComponent =
let alertViewComponent = AlertViewComponent()
alertViewComponent.render(with: .update(model: AlertViewComponent.Model(title: "title", message: "message")))
return alertViewComponent
()
但是当我显示警报时,我得到了错误:
'UIAlertController 必须有标题、消息或动作 显示'
【问题讨论】:
来自 UIAlertController 文档:The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.
。可以改用extension
吗?
所以你的课程is-a
和has-a
UIAlertViewController - 听起来很可疑。
@Larme 我明白了......我想我会调查一下
@Gereon 首先我将 UIViewController 子类化,然后我更改了,但我猜这不起作用
你真正想做的是:let alert =
UIAlert.init(withMyCustomModel: model)
,然后在UIAlertController
上添加一个扩展,不是吗?还是你也想处理其他事情?究竟是哪一个?
【参考方案1】:
您从未调用过您的 configureAlert
函数。此外,您是子类化并且还有一个警报控制器成员。介绍了哪一个?根本不需要子类化警报控制器 - 只需使用您的成员即可。
【讨论】:
是的,需要修复的东西太多了。不知道我是否应该标记这个答案...以上是关于我正在继承 AlertController 并收到错误“UIAlertController must have a title, a message or an action to display”的主要内容,如果未能解决你的问题,请参考以下文章
AlertController - 与 UIViewController 冲突
如何在 ionic 4 中使用 alertcontroller 返回承诺?
在 AlertController 中从 UIPickerView 填充文本字段时出现问题
Swift - 如何在自定义 AlertController 中点击按钮时呈现 ViewController