我正在开发项目,我必须根据datepicker值设置按钮标题。
我的设计如下
现在我的代码如下所示
这是我设置为tableviewCell
的按钮 let btn_start_time = cell.viewWithTag(2) as! UIButton
btn_start_time.addTarget(self, action: #selector(btnStartTimePressed), forControlEvents: .TouchUpInside)
点击
时,这是我的按钮操作方法func btnStartTimePressed(sender: UIButton!) {
let buttonPosition = sender.convertPoint(CGPointZero, toView: self.tbl_availability)
let indexPath = self.tbl_availability.indexPathForRowAtPoint(buttonPosition)
UtilityHandler.addpopupZoomAnimationTopickerView(view_time_picker, view: self.view)
time_picker.addTarget(self, action: Selector(datepickeractionmethod(time_picker)), forControlEvents: .ValueChanged)
sender.setTitleColor(BUTTON_BLACK_COLOR_222222, forState: .Normal)
sender.setTitle(strDate as String, forState: .Normal)
}
这是我的datepicker Action方法
func datepickeractionmethod(sender : UIDatePicker) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "hh:mm a"
strDate = dateFormatter.stringFromDate(self.time_picker.date)
}
我想在更改datepicker值时更改按钮标题。
我试过了 1)我将额外的参数作为按钮添加到datepicker动作方法并在那里设置标题但是当我点击按钮时它也改变了,而不是当我的DATEPICKER值改变时 2)我尝试设置按钮动作方法但是当我点击按钮时它起作用。
请帮帮我。
答案 0 :(得分:1)
创建一个枚举并使其实例检查当前显示的是哪个选择器是startTime还是endTime,还创建两个String实例来保存选择器中的值。
enum TimeType {
case StartTime
case EndTime
}
var timeType: TimeType?
var startTime = "Start Time"
var endTime = "End Time"
现在在cellForRowAt indexPath
中使用这两个字符串对象。
let btn_start_time = cell.viewWithTag(2) as! UIButton
btn_start_time.addTarget(self, action: #selector(btnStartTimePressed), forControlEvents: .TouchUpInside)
btn_start_time.setTitle(startTime, for: .normal)
let btn_start_time = cell.viewWithTag(3) as! UIButton
btn_start_time.addTarget(self, action: #selector(btnEndTimePressed), forControlEvents: .TouchUpInside)
btn_start_time.setTitle(endTime, for: .normal)
现在在按钮操作中设置枚举实例。
func btnStartTimePressed(sender: UIButton!) {
UtilityHandler.addpopupZoomAnimationTopickerView(view_time_picker, view: self.view)
time_picker.addTarget(self, action:#selector(datepickeractionmethod(_:)), forControlEvents: .ValueChanged)
//Set which picker is Shown
timeType = .StartTime
}
func btnEndTimePressed(sender: UIButton!) {
let buttonPosition = sender.convertPoint(CGPointZero, toView: self.tbl_availability)
let indexPath = self.tbl_availability.indexPathForRowAtPoint(buttonPosition)
UtilityHandler.addpopupZoomAnimationTopickerView(view_time_picker, view: self.view)
time_picker.addTarget(self, action: #selector(datepickeractionmethod(_:)), forControlEvents: .ValueChanged)
//Set which picker is Shown
timeType = .EndTime
}
现在在datePicker操作中使用此枚举
func datepickeractionmethod(sender : UIDatePicker) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "hh:mm a"
let time = dateFormatter.stringFromDate(self.time_picker.date)
if self.timeType = .StartTime {
self.startTime = time
}
else {
self. endTime = time
}
//Now simply reload tableView or you can even reload single row that contains these two buttons.
self.tableView.reloadData()
}
答案 1 :(得分:0)
您需要为跟踪点击按钮声明全局按钮实例。
class ViewController: UIViewController{
var clickedButton : UIButton
func btnStartTimePressed(sender: UIButton!) {
let buttonPosition = sender.convertPoint(CGPointZero, toView: self.tbl_availability)
let indexPath = self.tbl_availability.indexPathForRowAtPoint(buttonPosition)
UtilityHandler.addpopupZoomAnimationTopickerView(view_time_picker, view: self.view)
time_picker.addTarget(self, action: Selector(datepickeractionmethod(time_picker)), forControlEvents: .ValueChanged)
sender.setTitleColor(BUTTON_BLACK_COLOR_222222, forState: .Normal)
clickedButton = sender
}
func datepickeractionmethod(sender : UIDatePicker) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "hh:mm a"
strDate = dateFormatter.stringFromDate(self.time_picker.date)
clickedButton.setTitle(strDate as String, forState: .Normal)
}