UIDate Picker valuechanged不会更新第一个旋转,但每次旋转后 [英] UIDate Picker valuechanged does not update the first spin, but does every spin after.. iOS

查看:74
本文介绍了UIDate Picker valuechanged不会更新第一个旋转,但每次旋转后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌入在一个静态TableViewCell中的UIDate Picker,目前我禁用了除代码日期选择器代码以外的大多数代码。

I have a UIDate Picker embedded in a static TableViewCell and at the moment I disabled most of the code except the code responsible for the date picker.

我使用日期选择器作为倒数计时器

所以这是一切,除了一些常见的出口和vars:

So this is kind of all, except some usual outlets and vars:

override func viewDidLoad() {
    super.viewDidLoad()

    //  tfName.delegate = self
    //  tfDescription.delegate = self
    //

    datePicker.countDownDuration = 60

    //  pickerValueChanged(self)

}


@IBAction func pickerValueChanged(sender: AnyObject) {

     seconds  = Int(datePicker.countDownDuration)
     hours  = seconds / 3600
    if hours == 0{
        minutes = seconds / 60
    } else{
        minutes = seconds % 3600 / 60
    }
    labelDuration.text = "\(hours) hours \(minutes) minutes"
}

我第一次更改Picker的值时,改变的值函数根本不会触发,但是在没有失败的情况下转动。

The first time I change the value of the Picker, the value changed function does not fire at all, but the spins after it does without failing.

我试图在 viewDidLoad 中调用它,使用 pickerValueChanged(self),但这样做有效,但是不解决问题。另外,我已经在 pickerValueChanged 函数和 viewDidLoad 中的每行代码都注释了,但是它仍然没有触发第一个旋转。 。

I tried to call it in viewDidLoad, with pickerValueChanged(self) and that works, but does not solve the problem. Also I commented out every line of code in the pickerValueChanged function and viewDidLoad but it still did not fire the first spin..

谢谢你的时间!

更新:添加图片

第一次旋转后,pickerValueChanged不会调用:

After the first spin, pickerValueChanged does not get called:

从第二次旋转和超越,事件被调用并且标签被更新:

From the second spin and beyond, the event gets called and the label is updated:

尝试:

执行:
datePicker .setDate(NSDate(),动画:中的true viewDidLoad()
解决了事件不被调用的问题第一个旋转,但是这会将日期选择器的初始状态设置为当前时间。由于我使用此控件来设置事件的持续时间,我想让它从0小时1分钟开始
这可以通过 datePicker.countDownDuration = 60 完成,但此后,主要问题再次出现。
所以一个解决方案可能是使用NSDate设置DatePicker的1分钟,但如何做?

Executing: datePicker.setDate(NSDate(), animated: true in viewDidLoad() solves the problem that the event doesn't get called at the first spin, but this sets the initial state of the Date Picker to the current time. Since I'm using this control to set a duration to a event, I want to let it start at 0 hour and 1 minute. This can be done with datePicker.countDownDuration = 60 but after this, the main problem gets back again. So a solution could be setting the DatePicker with a NSDate of 1 minute, but how to do this?

解决方案:

var dateComp : NSDateComponents = NSDateComponents()
dateComp.hour = 0
dateComp.minute = 1
dateComp.timeZone = NSTimeZone.systemTimeZone()
var calendar : NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!
var date : NSDate = calendar.dateFromComponents(dateComp)!

datePicker.setDate(date, animated: true)


推荐答案

我提出了以下解决方案,以0小时1分钟初始化datePicker组件(倒计时模式),并在第一次旋转时直接响应。因为这似乎是一个错误,如果您希望在datePicker更改时更改其值,并且不会在第一次执行时更改其值,则非常令人沮丧:

I came up the following solution to initialize the datePicker component (in countdown timer mode) with 0 hours and 1 minute and it responds directly at the first spin. Since this appears to be a bug and is very frustrating if you want a textlabel to update its value when the datePicker is changed and it does not at the first go:

var dateComp : NSDateComponents = NSDateComponents()
dateComp.hour = 0
dateComp.minute = 1
dateComp.timeZone = NSTimeZone.systemTimeZone()
var calendar : NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!
var date : NSDate = calendar.dateFromComponents(dateComp)!

datePicker.setDate(date, animated: true)

timeZone组件这不是真的需要为我的实现工作。

The timeZone component is not really needed for this to work for my implementation.

这篇关于UIDate Picker valuechanged不会更新第一个旋转,但每次旋转后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆