user6876907
user6876907

Reputation:

UIPickerView selected time to run code

I have added in a UIPickerView and currently have it store the selected time as a string. I want the app to carry out a simple line of code when the time that was selected on the pickerview is the time in the real world. Here is the code that I have added.

For the Clock, used to find the real world time:

let clockString: String = formatADate()
func formatADate() -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .short
    dateFormatter.dateFormat = "hh:mm:ss a"
    let date = NSDate()
    let output = dateFormatter.string(from: date as Date)
    print(output)
    return output
}

Here is the code for the UIPickerView:

@IBOutlet var dateTimeDisplay: UILabel!
@IBOutlet var datePicker: UIDatePicker!
@IBAction func datePickerchanged(_ sender: Any) {
    setDateAndTime()
}
func setDateAndTime() {
    let formatter = DateFormatter()
    formatter.dateFormat = "hh:mm:ss a"
    _ = formatter.string(from: datePicker.date)
    str = dateFormatter.string(from: (datePicker?.date)!)
    dateTimeDisplay.text = str
}

And here is what I want to happen when the selected time and the real world time match up:

takePhoto = true

Upvotes: 0

Views: 375

Answers (1)

BHAVIK
BHAVIK

Reputation: 899

When the pick date the start one timer function

call the function in picker

var timercount = Timer()

viewdidload()
{
 timercount = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(Check), userInfo: nil, repeats: true)
}


     Func Check()
    {

     let nowdate = NSDate()//two date declare global Var
     let date2 = datePicker?.date //chek the Time How Much Time Remain
     let elapsed = date2?.timeIntervalSince(nowdate as Date)

     if Int(elapsed!) == 0
          {

            takePhoto = true
          }

    }

Upvotes: 1

Related Questions