Skip to content

Instantly share code, notes, and snippets.

@d-date
Last active February 18, 2017 16:06
Show Gist options
  • Save d-date/79261150a8eae0804c2ef337c3127b16 to your computer and use it in GitHub Desktop.
Save d-date/79261150a8eae0804c2ef337c3127b16 to your computer and use it in GitHub Desktop.
import UIKit
//でもこれだと、styleの指定ができないので、3つのタプルにしてみた
private func showAlert(title: String, message: String, actions: [(String, UIAlertActionStyle, ((UIAlertAction) -> Void)?)]) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
actions.forEach {
let action = UIAlertAction(title: $0, style: $1, handler: $2)
alertController.addAction(action)
}
present(alertController, animated: true, completion: nil)
}
func onTapped(_ sender: Any) {
var items = [(String, UIAlertActionStyle, ((UIAlertAction) -> Void)?)]()
items.append(("あ", .cancel,{ _ in print("あ") }))
items.append(("い", .default, nil))
items.append(("う", .default, { _ in print("う") }))
items.append(("え", .default, nil))
items.append(("お", .default, { _ in print("お") }))
showAlert(title: "タイトル", message: "メッセージ", actions: items)
}
import UIKit
//タイトルとアクションをペアにしたいというので、タプルにしてみた
private func showAlert(title: String, message: String, actions: [(String, ((UIAlertAction) -> Void)?)]) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
actions.forEach {
let action = UIAlertAction(title: $0, style: .default, handler: $1)
alertController.addAction(action)
}
present(alertController, animated: true, completion: nil)
}
func onTapped(_ sender: Any) {
var items = [(String, ((UIAlertAction) -> Void)?)]()
items.append(("あ", { _ in print("あ") }))
items.append(("い", nil))
items.append(("う", { _ in print("う") }))
items.append(("え", nil))
items.append(("お", { action in
//actionで何かできるなど
print("お")
}))
showAlert(title: "タイトル", message: "メッセージ", actions: items)
}
import UIKit
//でも結局これって、UIAlertActionの配列でよくない?という結論
private func showAlert(title: String, message: String, actions: [UIAlertAction]) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
actions.forEach {
let action = UIAlertAction(title: $0, style: $1, handler: $2)
alertController.addAction(action)
}
present(alertController, animated: true, completion: nil)
}
func onTapped(_ sender: Any) {
var items = [UIAlertAction]()
items.append(UIAlertAction(title: "あ", style: .cancel, handler: { _ in print("あ") } ))
items.append(UIAlertAction(title: "い", style: .cancel, handler: nil ))
items.append(UIAlertAction(title: "う", style: .cancel, handler: { _ in print("う") } ))
items.append(UIAlertAction(title: "え", style: .cancel, handler: nil ))
items.append(UIAlertAction(title: "お", style: .cancel, handler: { _ in print("お") } ))
showAlert(title: "タイトル", message: "メッセージ", actions: items)
}
@bannzai
Copy link

bannzai commented Feb 18, 2017

押した後のイベント以外(title, style)はだいたい共通で使う気がするのでenumを使っての定数化とprotocolを使った抽象化で下記のような感じはどうでしょう?

protocol AlertAction {
    var action: UIAlertAction { get }
}

enum CommonAlertActionType: AlertAction { // 共通
    typealias Handler = ((UIAlertAction) -> Void)
    
    case ok(Handler?)
    case cancel(Handler?)
    
    var action: UIAlertAction {
        switch self {
        case .ok(let handler):
            return UIAlertAction(title: "ok", style: .default, handler: handler)
        case .cancel(let handler):
            return UIAlertAction(title: "cancel", style: .cancel, handler: handler)
        }
    }
}

enum HogeAlertActionType: AlertAction { // 画面ごとに or 何かの機能単位
    typealias Handler = ((UIAlertAction) -> Void)
    
    case a(Handler?)
    case i(Handler?)
    case u(Handler?)
    case e(Handler?)
    case o(Handler?)
    
    var action: UIAlertAction {
        switch self {
        case .a(let handler):
            return UIAlertAction(title: "a", style: .default, handler: handler)
        case .i(let handler):
            return UIAlertAction(title: "i", style: .default, handler: handler)
        case .u(let handler):
            return UIAlertAction(title: "u", style: .default, handler: handler)
        case .e(let handler):
            return UIAlertAction(title: "e", style: .default, handler: handler)
        case .o(let handler):
            return UIAlertAction(title: "o", style: .default, handler: handler)
        }
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    private func showAlert(
        title: String,
        message: String,
        actions: [AlertAction]
        ) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        
        actions.forEach {
            alertController.addAction($0.action)
        }
        present(alertController, animated: true, completion: nil)
    }    
    func onTapped(_ sender: Any) {
        var items: [AlertAction] = []
        items.append(HogeAlertActionType.a({ _ in print("a") }))
        items.append(HogeAlertActionType.i(nil))
        items.append(HogeAlertActionType.u(nil))
        items.append(HogeAlertActionType.e(nil))
        items.append(HogeAlertActionType.o(nil))
        items.append(CommonAlertActionType.ok(nil))
        showAlert(title: "タイトル", message: "メッセージ", actions: items)
    }
    @IBAction func tapped(_ sender: Any) {
        onTapped(sender)
    }
}

って思って書いたが、そこまでスッキリしませんね。。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment