0

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

1 Answers1

1

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
          }

    }
BHAVIK
  • 890
  • 7
  • 35
  • I saw that he also had a UIlabel, how would you show the selected time on that – Oren Edrich Feb 17 '17 at 15:42
  • This code works as a timer. When the selected time on the PickerView match the current, real, world time, i want the code to be implemented –  Feb 17 '17 at 16:43
  • im not sure if your code is all code or if you used parenthesis to try to explain something. If you are trying to explain something try using /// to separate it. –  Feb 17 '17 at 17:39
  • Please update your code!!! im not sure if your code is all code or if you used parenthesis to try to explain something. If you are trying to explain something try using /// to separate it. –  Feb 21 '17 at 05:52