【发布时间】:2017-12-12 06:26:25
【问题描述】:
我的应用程序中有多个视图控制器。在它们中的每一个中,我都必须根据某些条件显示警报。我没有在每个控制器中添加警报控制器,而是尝试使用继承,如下所示。
UIExtension.swift
class UIExtension: UIViewController {
func prepareAlert(title: String, message: String) -> UIAlertController {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
return alert
}
}
FirstViewController.swift
class FirstViewController: UIExtension {
//somewhere inside used the following
present(prepareAlert(title: "Error Validation", message: "invalid fields"), animated: true, completion: nil)
}
同样,在其他视图控制器中使用 UIExtension 来显示警报。推荐这种方式吗?
【问题讨论】:
标签: ios swift inheritance uiviewcontroller