微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift开发笔记11 - 使用UIAlertController在底部弹出日期选择框

点击日期按钮,弹出一个日期选择框在底部,选完日期后,修改日期按钮上的文字


关于日期按钮的外观绘制详见上一节,点击按钮的响应事件为:

 @IBAction func selectDate(sender: AnyObject) {
        
        let alertController:UIAlertController=UIAlertController(title: "\n\n\n\n\n\n\n\n\n\n\n\n",message: nil,preferredStyle: UIAlertControllerStyle.ActionSheet)
        // 初始化 datePicker
        let datePicker = UIDatePicker( )
        //将日期选择器区域设置为中文,则选择器日期显示中文
        datePicker.locale = NSLocale(localeIdentifier: "zh_CN")
        // 设置样式,当前设为同时显示日期和时间
        datePicker.datePickerMode = UIDatePickerMode.Date
        // 设置认时间
        datePicker.date = NSDate()
        // 响应事件(只要滚轮变化就会触发)
        // datePicker.addTarget(self,action:Selector("datePickerValueChange:"),forControlEvents: UIControlEvents.ValueChanged)
        alertController.addAction(UIAlertAction(title: "确定",style: UIAlertActionStyle.Default){
            (alertAction)->Void in
            print("date select: \(datePicker.date.description)")
            //获取上一节中自定义的按钮外观DateButton类,设置DateButton类属性thedate
            let myDateButton=self.Datebutt as? DateButton
            myDateButton?.thedate=datePicker.date
            //强制刷新
            myDateButton?.setNeedsdisplay()
            })
        alertController.addAction(UIAlertAction(title: "取消",style: UIAlertActionStyle.Cancel,handler:nil))
        
        alertController.view.addSubview(datePicker)
        
        self.presentViewController(alertController,animated: true,completion: nil)
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐