LoginSignup
1
0

More than 1 year has passed since last update.

すぐにアラートを作る為に (UIAlertController)

Last updated at Posted at 2021-09-15

今回の内容

  • 3種類のアラートをすぐ作れる様にカスタム前の状態で、書き残しておきます。

コードと表示結果

アラート1(preferredStyle: .alert) 

BD3DDCDB-0A84-4DE4-BBCE-950795356A40_1_201_a.jpeg


        let usualAlert = {() -> UIAlertController in

            let alert = UIAlertController(title: "タイトルだよ", message: "アラートの内容だよ", preferredStyle: .alert)

            alert.addAction(UIAlertAction(title: "No", style: .destructive, handler: { _ in

                print("No")     
            }))

            alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { _ in

                print("Yes")                
            }))

            return alert
        }()

        self.present(usualAler, animated: true, completion: nil)

アラート2(preferredStyle: .actionSheet) 

206C76A8-4522-4C54-BBBE-211E3F15A2DB_1_201_a.jpeg

        let usualActionSheetAlert = {() -> UIAlertController in

            let alert = UIAlertController(title: "タイトルだよ", message: "アラートの内容だよ", preferredStyle: .actionSheet)

            alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { _ in

                print("Yes")                
            }))

            alert.addAction(UIAlertAction(title: "No", style: .destructive, handler: { _ in

                print("No")                
            }))

            alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in

                print("cancel")                
            }))

            return alert
        }()

        self.present(usualActionSheetAlert, animated: true, completion: nil)

アラート3(preferredStyle: .alert) TextFieldを表示

DFCD3B9F-C6FD-4318-98FC-6CC2F60F588E_1_201_a.jpeg

        let textfieldInAlert = {() -> UIAlertController in

            let alert = UIAlertController(title: "タイトルだよ", message: "アラートの内容だよ", preferredStyle: .alert)

            alert.addTextField { (alertTextField:UITextField) in

                alertTextField.placeholder = "文字を入力してね"

            }

            alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { _ in

                print("Yes")

            }))

            alert.addAction(UIAlertAction(title: "No", style: .destructive, handler: { _ in

                print("No")

            }))

            return alert
        }()

        self.present(textfieldInAlert, animated: true, completion: nil)

終わり

ご指摘、ご質問などありましたら、コメントまでお願い致します。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0