LoginSignup
4
2

More than 3 years have passed since last update.

コードで書く画面遷移 アラート swift

Last updated at Posted at 2019-08-12

『はい』、『いいえ』画面遷移

スクリーンショット 2019-08-13 2.00.46.png

はじめに、、、

alart.swift
 override func viewDidAppear(_ animated: Bool) {
この一行をかき、アラートが画面に見えてる状態にします

タイトルとメッセージ(上二段)

alart.swift
        let matchAlert : UIAlertController = UIAlertController(title: "マッチしました", message: "トークをはじめますか?", preferredStyle: UIAlertController.Style.alert)

「はい」の場合

alert.swift
     let yesAction : UIAlertAction = UIAlertAction(title: "はい", style: UIAlertAction.Style.default){
            action in
            let ChatController = chatController()
            self.present(UINavigationController(rootViewController: ChatController),animated: true , completion: nil)
        }
初めに、「はい」をletの後に ”yes action” と定義します。
次に、"title"に表示されるメッセージを記入します。
ここから画面遷移についてです。
"action in"の後のletに遷移先のコントローラを好きな名前に定義します。
今回の場合”Chatcontroller”と定義し遷移先のview controller名を”=”の後に書きます。
今回,遷移先のview controllerの名前が”chatController”ですので"="の後、
”chat controller”と書きます。
この時、後ろに () を忘れず書いてください。
又、定義名とコントローラ名は同じ名前は使用できません。
そのため、定義の方の”c”を大文字に変えています。
上で定義したコントローラ名を"rootViewController"の後ろに書きます。
今回の"Chatcontroller"ですので上記のように書きます。
この時presentの前にselfをつけ忘れたらエラーが出たので忘れずにつけてください。

「いいえ」も上記と同じように書きますが"action in"の後の定義コントローラ名が被らないよう、気をつけてください。又遷移先が異なるため、"chatController"のところをまた別の遷移先のviewController名に変えてください。

最後に、、

alert.swift
  matchAlert.addAction(yesAction)
        matchAlert.addAction(noAction)
        present(matchAlert, animated:true, completion: nil)
    }
"match alert"はタイトルとメッセージで定義したものです。
()にそれぞれの選択肢の存在を書いてください

まとめ

alert.swift
    override func viewDidAppear(_ animated: Bool) {

        let matchAlert : UIAlertController = UIAlertController(title: "マッチしました", message: "トークをはじめますか?", preferredStyle: UIAlertController.Style.alert)

        let yesAction : UIAlertAction = UIAlertAction(title: "はい", style: UIAlertAction.Style.default){
            action in
            let ChatController = chatController()
            self.present(UINavigationController(rootViewController: ChatController),animated: true , completion: nil)
        }

        let noAction : UIAlertAction = UIAlertAction(title: "いいえ", style: UIAlertAction.Style.cancel){
            action in
            let noController = HomeController()
            self.present(UINavigationController(rootViewController: noController), animated: true, completion: nil)
        }

        matchAlert.addAction(yesAction)
        matchAlert.addAction(noAction)
        present(matchAlert, animated:true, completion: nil)


    }
以上の記事はプログラミング初心者によって書かれているため、コードが間違っている場合がございます。
もし、そのようであれば教えて頂けると幸いです
4
2
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
4
2