ActionSheetの実装の仕方[Xcode/Storyboard]


アプリを使っていると、カメラかアルバムを選択するシートをみたことがあるかと思います。
Xcode/Storyboardでどのように実装するかを簡単に実装して記事にしました。

環境

・Mac Book Pro(macOS:BigSur)
・Xcode(ver:12.5)

実装例

コード例

ViewController.swift
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }


    @IBAction func showAlert(_ sender: Any) {

        let alertController = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .actionSheet)

        let action1 = UIAlertAction(title: "タイトル1", style: .default) { alert in
            print("Action1")
        }

        let action2 = UIAlertAction(title: "タイトル2", style: .default) { (alert) in
            print("Action2")
        }

        let action3 = UIAlertAction(title: "キャンセル", style: .cancel, handler: nil)

        alertController.addAction(action1)
        alertController.addAction(action2)
        alertController.addAction(action3)
        self.present(alertController, animated: true, completion: nil)
    }
}