Swift中UIAlertController的用法


作為一個已經有了一部分經驗的iOS開發人員,對於蘋果公司的一些新動向,當然要密切關注了,從2014年發布swift語言以來,雖然才僅僅兩年,但是這門語言的強大,已經吸引了越來越多的開發人員,我也是最近才開始學習swift語言,只是希望將自己的學習做一些紀錄。

相信所以做iPhone手機開發的程序員,都曾經使用過UIAlertView這個控件。但是在iOS9中UIAlertView這個控件被UIAlertController所取代,雖然UIAlertView暫時還沒有被完全廢棄,但是很明顯這不過是早晚的事情。

swift中的UIAlertController和OC中並沒有太大的區別,用法也非常的簡單。

     let alertController = UIAlertController(title: "通知", message: "確定還是取消", preferredStyle: UIAlertControllerStyle.Alert) // 這里因為控件都不存在改變的可能,所以一律使用let類型

        let alertView1 = UIAlertAction(title: "確定", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in

            print("確定按鈕點擊事件")

        }

        let alertView2 = UIAlertAction(title: "取消", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in

            print("取消按鈕點擊事件")

        }

        let alertView3 = UIAlertAction(title: "下次吧", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in

            print("下次吧按鈕點擊事件")

        }
        alertController.addAction(alertView1)

        alertController.addAction(alertView2)

        alertController.addAction(alertView3) // 當添加的UIAlertAction超過兩個的時候,會自動變成縱向分布

        self.presentViewController(alertController, animated: true, completion: nil)

 

總體來說,同OC的差距並不大,只是語法上的區別,比較明顯多是UIAlertActionStyle.Default,swift中所有的類型選擇,均采取點語法的方式,在沒有向OC中UIAlertActionStyleDefault的寫法。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM