【Swift】alertControllerに3つ以上のalertActionを実装してみた

解説

Alertは画面上に表示される警告表示。

f:id:nekokichi_yos2:20181203202342p:plain

 

一般的なアプリでは、AlertActionの数は

  • 2個
  • 3個

がほとんど。

 

しかし、もしAlertActionの数を2,3,4…と増やしていったらどうなるのか?

 

実験結果

 

※4個

f:id:nekokichi_yos2:20181203202412p:plain

 

※12個

f:id:nekokichi_yos2:20181203202429p:plain

 

※15個

f:id:nekokichi_yos2:20181203202450p:plain

 

13,4個以降は、スクロールできる以外変化なし。

 

 

ソースコード

 

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func alertButton(_ sender: Any) {
        alert()
    }
    
    func alert() {
        let alertController = UIAlertController(title: "アラートです", message: "どれか選択しろ", preferredStyle: .alert)
        let alertAction1 = UIAlertAction(title: "1", style: .default, handler: nil)
        let alertAction2 = UIAlertAction(title: "2", style: .default, handler: nil)
        let alertAction3 = UIAlertAction(title: "3", style: .default, handler: nil)
        let alertAction4 = UIAlertAction(title: "4", style: .default, handler: nil)
        let alertAction5 = UIAlertAction(title: "1", style: .default, handler: nil)
        let alertAction6 = UIAlertAction(title: "2", style: .default, handler: nil)
        let alertAction7 = UIAlertAction(title: "3", style: .default, handler: nil)
        let alertAction8 = UIAlertAction(title: "4", style: .default, handler: nil)
        let alertAction9 = UIAlertAction(title: "1", style: .default, handler: nil)
        let alertAction10 = UIAlertAction(title: "2", style: .default, handler: nil)
        let alertAction11 = UIAlertAction(title: "3", style: .default, handler: nil)
        let alertAction12 = UIAlertAction(title: "4", style: .default, handler: nil)
        let alertAction13 = UIAlertAction(title: "2", style: .default, handler: nil)
        let alertAction14 = UIAlertAction(title: "3", style: .default, handler: nil)
        let alertAction15 = UIAlertAction(title: "4", style: .default, handler: nil)
        alertController.addAction(alertAction1)
        alertController.addAction(alertAction2)
        alertController.addAction(alertAction3)
        alertController.addAction(alertAction4)
        alertController.addAction(alertAction5)
        alertController.addAction(alertAction6)
        alertController.addAction(alertAction7)
        alertController.addAction(alertAction8)
        alertController.addAction(alertAction9)
        alertController.addAction(alertAction10)
        alertController.addAction(alertAction11)
        alertController.addAction(alertAction12)
        alertController.addAction(alertAction13)
        alertController.addAction(alertAction14)
        alertController.addAction(alertAction15)
    }

}