【Xcode/Swift】シンプルなアラート画面を表示する方法

Storyboard

ViewController.swift

import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    //アラート画面を表示させる
    private func showAlert() {
        let alert = UIAlertController(title: "Alert画面", message: "画面を閉じても良いですか?", preferredStyle: .alert)
        
        let delete = UIAlertAction(title: "はい", style: .default, handler: { (action) -> Void in
            //【はい】を押したときの処理
            print("【はい】が押された")
        })
        
        let cancel = UIAlertAction(title: "キャンセル", style: .cancel, handler: { (action) -> Void in
            //【キャンセル】を押したときの処理
            print("【キャンセル】が押された")
        })
        
        alert.addAction(delete)
        
        alert.addAction(cancel)
        
        self.present(alert, animated: true, completion: nil)
    }
    
    @IBAction func showAlertAction(_ sender: Any) {
        showAlert()
    }
    
    
}

この記事は役に立ちましたか?

はい
いいえ
貴重なフィードバックありがとうございます!