Swift中UIAlertController的使用

来源:互联网 发布:免费开源o2o系统源码 编辑:程序博客网 时间:2024/05/16 17:34
在swift中alertView的初始化只允许创建拥有一个取消按钮的对话框视图
          //定义一个UIAlertControll
 var alert =UIAlertController(title:"添加内容", message: "请输入。。。", preferredStyle: UIAlertControllerStyle.Alert)
        //创建下面的保存按钮以及按钮的样式
        let saveAction  =UIAlertAction(title:"保存", style: UIAlertActionStyle.Default) { (action:UIAlertAction!) ->Voidin     
          //创建一个textFiled对象,用于输入内容
            let textfiled = alert.textFields![0]asUITextField
          
        }
        //创建一个取消按钮
        let cancelAction =UIAlertAction(title:"取消", style: UIAlertActionStyle.Default) { (action:UIAlertAction!) ->Voidin
           
        }
        //讲这两个添加上去
        alert.addAction(saveAction)
        alert.
addAction(cancelAction)
       
         //添加文本
        alert.addTextFieldWithConfigurationHandler { (textFiled:UITextField!) -> Voidin
           
        }
        //让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)
0 0
原创粉丝点击