作為一個已經有了一部分經驗的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的寫法。