- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在努力让我的生活更轻松一些,让我的应用程序更易于维护,同时减少重复代码的数量。所以我认为将一些代码用于显示特定类型的 UIAlertController 到它自己的类中会很好。
问题是,我有基本相同的警报,只是根据我在我的应用程序中显示它的位置而略有不同。所以我心想:为什么不使用枚举,每次我想显示那种警报时,只需给它枚举值。摆脱我代码中所有重复的字符串。
我喜欢 Swift 中的枚举。他们太狡猾了——嗯,我是说敏捷。所以我想出了这个枚举示例:
enum MyAlertType {
case a
case b
}
接下来我创建了一个简单的类来处理显示:
class MyAlert {
static func showAlert(ofType type: MyAlertType, inViewController viewController: UIViewController, handler: ((UIAlertAction) -> ())? = nil, completion: (() -> ())? = nil) {
var message: String
switch type {
case .a:
message = "A is a nice letter!"
case .b:
message = "B is a nice letter!"
}
let alert = UIAlertController(title: "Do you know which letter is nice?", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))
viewController.present(alert, animated: true, completion: completion)
}
}
//Somewhere in my code (obviously a UIViewController)
MyAlert.showAlert(ofType: .a), inViewController: self)
但是等等,我仍然需要为这个函数提供我想在其中显示警报的 ViewController。对我来说,这总是相同的( self )所以下一个合乎逻辑的步骤是使它成为一个扩展:
extension UIViewController {
func showAlert(ofType type: MyAlertType, handler: ((UIAlertAction) -> ())? = nil, completion: (() -> ())? = nil) {
var message: String
switch type {
case .a:
message = "A is a nice letter!"
case .b:
message = "B is a nice letter!"
}
let alert = UIAlertController(title: "Do you know which letter is nice?", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))
present(alert, animated: true, completion: completion)
}
}
//Somewhere in my UIViewController)
showAlert(ofType: .a)
但这使得所有 UIViewController 都可以使用代码,即使是那些我不明确需要/不想显示那种警报的 Controller 。我的意思是,是的,当然,我是开发人员,我可以决定不使用它,但另一方面,尽可能多地隐藏所有内容并不总是一个好习惯吗?谁知道将来谁会加入我的开发团队并开始以我未曾想到的方式滥用我的漂亮代码?
前几天我了解了 Swifts 面向协议(protocol)的编程方法(老实说我现在还没有完全理解)现在我想,我也许应该把它做成一个带有默认实现的协议(protocol)然后让唯一的那些UIViewControllers 实现协议(protocol),我需要在其中显示此警报。
protocol MyAlertProtocol {
func showAlert(ofType type: MyAlertType, handler: ((UIAlertAction) -> ())?, completion: (() -> ())?)
}
extension MyAlertProtocol where Self : UIViewController {
func showAlert(ofType type: MyAlertType, handler: ((UIAlertAction) -> ())? = nil, completion: (() -> ())? = nil) {
var message: String
switch type {
case .a:
message = "A is a nice letter!"
case .b:
message = "B is a nice letter!"
}
let alert = UIAlertController(title: "Do you know which letter is nice?", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))
present(alert, animated: true, completion: completion)
}
}
extension MyViewController: MyAlertProtocol {}
我知道这听起来像是一个基于意见的问题,所以我不是问你是否认为这个或那个更好,而是告诉我是否确实是 此场景的最佳实践及其外观。
通过协议(protocol)扩展默认实现的协议(protocol)?简单的 UIViewController 扩展?具有静态函数的自定义枚举、结构或类(如果是,是哪一个)?或者甚至可能只是 Swift 文件中某处的一个函数?我感到不知所措。选择的痛苦...
更新/解决方案
看完给出的答案后,我认为我的谢尔盖答案确实是最合适的。我想通过“减少重复代码行”来使我的喜欢“更容易”。对我来说,这包括我的 ViewController 中的“presend(controller:animated:)”。
但是,我认为你们是对的。我应该使用一个结构(一个静态函数实际上不需要一个类)和一个静态函数来生成 alert man 使它“准备好使用”但仍然让调用者决定在哪里呈现它。
通过这样做,我可以在任何我想要的地方使用我的警报生成结构,例如让一个代表展示它或传递它直到我到达一个可以展示它的 UIViewController,如果我的调用者不是一个.
因此在我非常简单的情况下,我会选择:
enum MyAlertType {
case a
case b
}
struct MyAlert {
static func showAlert(ofType type: MyAlertType, handler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
var message: String
switch type {
case .a:
message = "A is a nice letter!"
case .b:
message = "B is a nice letter!"
}
let alert = UIAlertController(title: "Do you know which letter is nice?", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))
return alert
}
}
感谢所有参与解除我脑海中的封锁的人。
最佳答案
制作静态函数是一种常见且方便的方法。请注意,您创建的类型仅用于命名空间。
Alec O 的回答很好,但有时您想要在按下 OK 按钮时传递一些您想要执行的操作。此外,我会选择结构而不是类。
这是我的警报版本:
struct Alert {
static func errorAlert(title: String, message: String?, cancelButton: Bool = false, completion: (() -> Void)? = nil) -> UIAlertController {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let actionOK = UIAlertAction(title: "OK", style: .default) {
_ in
guard let completion = completion else { return }
completion()
}
alert.addAction(actionOK)
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
if cancelButton { alert.addAction(cancel) }
return alert
}
在 ViewController
的子类中的用法:
// Creating alerts:
let simpleAlert = Alert.errorAlert(title: "Error", message: "Simple message")
let alertWithCompletionAndCancel = Alert.errorAlert(title: "Message", message: "Message", cancelButton: true) {
// do something awesome
}
// Presenting alerts:
present(simpleAlert, animated: true)
present(alertWithCompletionAndCancel, animated: true)
关于ios - 什么是通过 Enum 进行可重用 UIAlertController 配置的 Swift 最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44030213/
我们如何从第一个警报打开第二个 UIAlert? 第一段代码工作正常,显示了警报。但是我希望能够调用第二个警报 View ,如果选择了第一个警报中的一个选项,就会出现该 View 。 在下面的示例中,
我有一个 Parse Sign Up 和一个 UIAlertController,我希望 UIAlertController 显示一个指示器,但在注册出现错误时被另一个 UIAlertControll
如何在 UIAlertController 外部点击时关闭 UIAlertController? 我可以添加 UIAlertActionStyleCancel 样式的 UIAlertAction 来关
这是我的代码:请检查是否出了什么问题?请给我 UIActionsheet (UIAlertController) 的代码,其中包含 swift 中的数组 let alertA = UIAlertCon
我正在使用 UIAlertController 要求用户输入一个字符串,这个字符串不能为空。为此,我为位于 UIAlertController 上的 UITextField 上的每个编辑事件添加了处理
我想介绍一个 UIAlertController与 UIAlertControllerStyleAlert风格,同时解雇 UIAlertController与 UIAlertControllerSty
我正在使用 UIAlertController 获取用户输入并更新表格单元格。每次当我尝试创建警报时,我都会在控制台中收到以下警告 2015-11-19 17:51:42.034 SimpleTabl
我正在尝试将 UIAlertController 中的推送通知(PN)中的消息从 AppDelegate 呈现到当前 ViewController。如果我发送 1 个 PN,没有问题,并且会显示警报!
我正在尝试从 UIAlertController 文本字段中获取文本。有人可以告诉我我做错了什么,因为它不起作用。我得到一个零返回。 - (IBAction)btnMakeRec {
我正在将我的应用程序移植到 iOS 8.0 并注意到 UIAlertView 已被弃用。 所以我改变了使用 UIAlertController 的方法。这在大多数情况下都有效。 除了,当我的应用程序打
我想根据代码更改警报操作按钮的顺序,我尝试了所有可能性,但不允许我更改顺序。 根据 HIG,取消在右侧 https://developer.apple.com/library/ios/document
在 iOS 8 及更低版本中显示 UIActionSheet当键盘出现时,将在键盘上显示操作表。在 iOS 9 中,情况不再如此。 在我的应用程序中,我们有一个聊天功能,并希望通过键盘显示一个 Act
嗨,我想知道是否可以使用UIAlert播放声音?根据以下文章,这似乎是可能的,但我正在努力将Obj-C转换为Swift。任何帮助表示赞赏! IOS alert message with sound 最
我需要一个系统来隐藏所有UIAlertController当她进入后台时在我的应用程序中。目前我使用 BaseViewController我所有的类(class)UIViewControllers注册
我正在调用带有2个文本字段的UIAlertController(在Obj-C中)。我已经定义了需要在警报中使用的全局UITextField,因为我需要利用UITextFieldDelegate。 我的
我在 View 中添加了一个 UIAlterController。 IT 触发很好,看起来很好,并且有它的 Action ,但是点击取消 Action 什么都不做,它不运行任何代码,即使我添加一个 b
这个问题在这里已经有了答案: How to add textField in UIAlertController? (4 个回答) 4年前关闭。 我可以使用在 UIAlertController 中添
我正在开发一个带有深色主题的 iOS 应用程序,我想使用 UIAlertController .但是,白色警报看起来不合适。即使在黑暗主题的应用程序上使用黑暗主题是否违反人机界面指南 UIAlertC
我正在做一个使用 UIAlertView 的项目,现在的问题是我必须替换所有 UIAlertView's与 UIAlertController's代码中有大约 1250 个。我打算使用现有的 Util
我正在尝试使用操作表样式创建 UIAlertController,但我想将背景颜色更改为灰色。我已经能够找到一种方法来更改 UIAlertController 的背景颜色,但不能更改取消按钮。单独的取
我是一名优秀的程序员,十分优秀!