Jecky Jecky - 15 days ago 11
Swift Question

How to access value from Datepicker to change button title according to date picker value?

I am working on project in which I have to set button title by According to datepicker value.


My design is like below

THIS

Now my code is like below

This is button which I set into tableviewCell

let btn_start_time = cell.viewWithTag(2) as! UIButton
btn_start_time.addTarget(self, action: #selector(btnStartTimePressed), forControlEvents: .TouchUpInside)


This is my button action method when I clicked

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)
}


This is my datepicker Action method

func datepickeractionmethod(sender : UIDatePicker) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "hh:mm a"
strDate = dateFormatter.stringFromDate(self.time_picker.date)
}


I want to change button title when datepicker value is changed.

I tried

1) I take extra parameter as button into datepicker action method and set title there but it also changed when I clicked on button , NOT WHEN MY DATEPICKER VALUES CHANGE

2) I try to set into button action method but it worked when I clicked on button.

Please help me with it.

Answer

Make one enum and make its instance to check which picker is currently shown is it startTime or endTime, also create two String instance for holding the values from picker.

enum TimeType {
    case StartTime
    case EndTime
}

var timeType: TimeType?
var startTime = "Start Time"
var endTime = "End Time"

Now use this two strings object in 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)

Now set the enum instance in the button action.

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
}

Now use this enum in datePicker action

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() 
}