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的写法。

 

posted @ 2016-01-04 23:23  妄想孤独  阅读(4523)  评论(0编辑  收藏  举报