Last active
February 18, 2017 16:06
-
-
Save d-date/79261150a8eae0804c2ef337c3127b16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
押した後のイベント以外(title, style)はだいたい共通で使う気がするので
enum
を使っての定数化とprotocol
を使った抽象化で下記のような感じはどうでしょう?って思って書いたが、そこまでスッキリしませんね。。。