您的位置:首页 > 移动开发 > Swift

Swift中UIAlertController的使用

2015-03-27 16:53 134 查看
在swift中alertView的初始化只允许创建拥有一个取消按钮的对话框视图
          //定义一个UIAlertControll
 var alert =
UIAlertController(title:
"添加内容",
message: "请输入。。。",
preferredStyle: UIAlertControllerStyle.Alert)
        //创建下面的保存按钮以及按钮的样式
        let saveAction  =
UIAlertAction(title:
"保存",
style: UIAlertActionStyle.Default)
{ (action:UIAlertAction!) ->
Void
in     
          //创建一个textFiled对象,用于输入内容

            let textfiled = alert.textFields![0]
as
UITextField
          
        }
        //创建一个取消按钮
        let cancelAction =
UIAlertAction(title:
"取消",
style: UIAlertActionStyle.Default)
{ (action:UIAlertAction!) ->
Void
in

           
        }
        //讲这两个添加上去
        alert.addAction(saveAction)

        alert.addAction(cancelAction)
       
         //添加文本

        alert.addTextFieldWithConfigurationHandler { (textFiled:UITextField!)
-> Void
in

           
        }
        //让alert弹出,显示出view控制器
        self.presentViewController(alert,
animated: true, completion:
nil)
相关注意的地方:
   1: UIAlertController(title: <#String?#>, message: <#String?#>, preferredStyle: <#UIAlertControllerStyle#>)
     preferredStyle及为alert显示样式,默认为default
   2:保存取消按钮都需要去创建响应的AlertAction,并且要对其进行addAction()添加
   3:文本框的创建需要创建textFileds[]数组(具体参看API)并对其 强制类型转换
   4:对文本框进行添加alert.addTextFieldWithConfigurationHandler
   5:最后让alert弹出如同show(),即self.presentViewController(alert,
animated: true, completion:
nil)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: