felixmp
felixmp

Reputation: 350

overwhelmed by UIDatePicker. trying to preset .hour and .minute for func in viewDidLoad()

i spend 2.5 hours searching google, i think i have never been on google search page number 10 in my entire life. untill today.

im a newbie with swift and i want the app to open the new viewcontroller with a UIDatePicker (datepickermode: time) preset to the time the user choose the last time he set a time with the datepicker. basically like the iphone alarm if that helps?

the struct 'Time' that contains .hour and .minute (integers) has its own class.

i want to have a tiny function that is executed with viewDidLoad() and sets the timepicker.

   @IBAction func timePicker(_ sender: UIDatePicker) {
    let picker = timePicker.date
    getTime(date: picker)
    print(globalTime.savedTime)
}

func oldTime(withTime: TimeStruct.Time) {

    var newTime = DateComponents()
    newTime.hour = withTime.hour
    newTime.minute = withTime.minute

    var newDate = Date()

    timePicker.calendar.

    //blabliblub i've lost it here. the code makes no sense anymore



}

func getTime(date: Date) {

let components = Calendar.current.dateComponents([.hour, .minute], from: date)
globalTime.savedTime.hour = components.hour!
globalTime.savedTime.minute = components.minute!

}

getTime saves the time once set. oldTime gives the previously set time to the picker when viewed again to change again.

the user sets a time and clicks a save button that saves the current hour and minute to a variable of type 'Time'

oh and if possible, please help me with the use of Date(), not NSDate().

thank you so much for your time!

Felix ✌️

Upvotes: 0

Views: 851

Answers (2)

felixmp
felixmp

Reputation: 350

hope this helps anyone. thanks for any further help :)

import UIKit

var globalTime = TimeStruct()

class Time_Setting: UIViewController {

func oldTime(withTime: TimeStruct.Time) {

    let newHour = withTime.hour
    let newMinute = withTime.minute
    let someDate = Date()

    let newTimePicker = timePicker.calendar.date(bySettingHour: newHour, minute: newMinute, second: 0, of: someDate)

    timePicker.date = newTimePicker!

    }
}

another file:

import UIKit

class TimeStruct {

struct Time {
    var hour: Int
    var minute: Int
}

var savedTime = Time(hour: 07, minute: 00)
}

Upvotes: 0

Cole Petersen
Cole Petersen

Reputation: 74

Have you tried using a normal picker instead? The date picker is a template with certain built in functions. However, a normal may be able to do all that you need it to do. For instance, simply create a normal picker with 3 components (0,1 and 2) which control Hour Minute and PM/AM by doing:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    self.Picker.delegate = self
    self.Picker.dataSource = self
    PickerData = [["0","1",/*...*/,"9"],["0","1",/*...*/,"9"],["AM","PM"]]
    self.Picker.selectRow(row: /*var*/, inComponent: /*component*/, animated: /*bool*/)
} /*Insert data from previous session above via a variable*/

and

func numberOfComponents(in: UIPickerView) -> Int {
    return 3
}

//Number of rows
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return PickerData[component].count
}

//Data to return for the row and column being read
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return PickerData[component][row]
}

//Catpure the picker view selection
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    // This method is triggered whenever the user makes a change to the picker selection.
    // The parameter named row and component represents what was selected.
}

Upvotes: -1

Related Questions