gpt4 book ai didi

ios - 以编程方式使用日期选择器更改按钮标题时,它始终会重置

转载 作者:行者123 更新时间:2023-11-28 05:32:16 26 4
gpt4 key购买 nike

我有一个标签为“输入时间”的按钮,当您单击该按钮时,它会退出键盘并显示日期选择器,当我更改日期值时,按钮上的文本会按预期更改为该日期。但是,如果我第二次更改日期,按钮标题将恢复为“输入时间”,如果我再次更改日期,它会正确更改为日期,但是当我第四次更改它时,它会返回到“输入时间” ",所以按钮文本会每隔一段时间重置一次。我不确定为什么会这样。请参阅下面的代码:

    override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
composeTextView.becomeFirstResponder()
addFunListButton.backgroundColor = UIColor(red: 0xf5/255, green: 0xa6/255, blue: 0x23/255, alpha: 1.0)
addFunListButton.tintColor = UIColor.whiteColor()

nowTimeButton.backgroundColor = UIColor(red: 0xe9/255, green: 0x7f/255, blue: 0x02/255, alpha: 1.0)
nowTimeButton.tintColor = UIColor.whiteColor()

enterTimeButton.backgroundColor = UIColor(red: 0xe9/255, green: 0x7f/255, blue: 0x02/255, alpha: 0.8)
enterTimeButton.tintColor = UIColor.whiteColor()

datePickerBox.addTarget(self, action: Selector("eventTime"), forControlEvents: UIControlEvents.ValueChanged)

// Setting up the date picker
let currentDate = NSDate()
datePickerBox.minimumDate = currentDate
datePickerBox.date = currentDate

}

@IBAction func datePickerBoxAction(sender: UIDatePicker) {
printDate(sender.date)
}

func printDate(date:NSDate){

let dateFormatter = NSDateFormatter()

var theDateFormat = NSDateFormatterStyle.ShortStyle
let theTimeFormat = NSDateFormatterStyle.ShortStyle

dateFormatter.dateStyle = theDateFormat
dateFormatter.timeStyle = theTimeFormat

NSLog("%@", date)
enterTimeButton.titleLabel?.text = dateFormatter.stringFromDate(date)
eventTime = date
}

最佳答案

不要直接设置标签文本。告诉按钮标签文本应该是什么:

let title = dateFormatter.stringFromDate(date)
enterTimeButton.setTitle(title, forState:UIControlState.Normal)

关于ios - 以编程方式使用日期选择器更改按钮标题时,它始终会重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27430321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com