2016-05-04 68 views
-1

我正在嘗試保存一些UIDatePickers的選定時間。我的日期選擇器被稱爲datePicker1。它目前設置爲只是小時和分鐘,我有一個「保存」按鈕,我想保存datePickers時間(小時和分鐘),並能夠在以後回調這些時間。我目前正在使用NSUserDefaults,但如果有更好的方法來做到這一點,讓我知道。 (日期與它無關,我正在創建一個應用程序,每天都會通知用戶,所以我所關心的是小時和分鐘數)。任何幫助將不勝感激。UIDatePicker快速保存時間

+0

加載數據,你的問題並不在要保存的時間是什麼格式規定(例如一個字符串?小時和分鐘的整數?一個NSDate對象?) –

+0

那麼你想要什麼? – Lion

回答

0

如果想要將小時和分鐘保存爲字符串,這可能會給您一些想法。該實施涉及的出口單的UIDatePicker和動作連接到一個「保存」按鈕:

class ViewController: UIViewController { 

@IBOutlet weak var datePicker: UIDatePicker! 

@IBAction func save(sender: UIButton) { 

    let dateOnPicker = datePicker.date //capture the date shown on the picker 

    let dateFormatter = NSDateFormatter() //create a date formatter 

    //if you want to convert time to string like "9:07 PM": 
    dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle //check out docs for NSDateFormatterStyle to see other styles 
    let timeAsString = dateFormatter.stringFromDate(dateOnPicker) 
    print(timeAsString) //ex. prints 3:04 AM as "3:04 AM" and 11:37 PM as "11:37 PM" 

    //if you want to convert time to string like "0627" (military time): 
    dateFormatter.dateFormat = "HHmm" //could also store as HH:mm if you want a colon in the string 
    let timeAsStringMilitaryTime = dateFormatter.stringFromDate(dateOnPicker) 
    print(timeAsStringMilitaryTime) //in miliatary time: ex. prints 3:04AM as "0304" and 11:37 PM as "2337" 

    //there are other options, but these are two that you might find useful! 

    //you could then save either version (whichever you end up using) to NSUserDefaults as a string; i went with the military time version since i think it's easier to parse, but just personal preference: 
    NSUserDefaults.standardUserDefaults().setObject(timeAsStringMilitaryTime, forKey: "SavedTime") 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    //when you start up (or whenever you want to retrieve the saved time), you could check to see if you have a time saved, and if so, parse it into hours and minutes (assuming you are saving the military time version): 

    if let savedTime = NSUserDefaults.standardUserDefaults().objectForKey("SavedTime") as? String { 

     //the "HHmm" military time format will ALWAYS be 4 digits long, and the first two characters represent the hours and the second two characters represent the minutes 

     let hoursRange = savedTime.startIndex...savedTime.startIndex.advancedBy(1) //refers to the first two digits of 4-digit time) 
     let hours = savedTime[hoursRange] 

     let minutesRange = savedTime.startIndex.advancedBy(2)...savedTime.endIndex.predecessor() //refers to the last two digits of 4-digit time) 
     let minutes = savedTime[minutesRange] 

     print(hours) 
     print(minutes) 
    } 
} 

希望這是有幫助的!

邁克

+0

邁克,所以我得到如何節省時間,現在如果我想用這段時間來觸發警報,並且當該頁面被重新加載時,日期選擇器再次顯示節省的時間,我將如何做到這一點? –

0

如果您使用的是UIDatePickerView:(情節板中設置時間) 下列功能保存/載入時間的NSDate和因爲你的UIDatePickerView設置爲時間,只有小時/分鐘就會顯示

定義一個變量,「時間」,其中保存您選擇的日期

var date :NSDate?  

使用此功能將數據保存到變量

func save(){ 
    date = datePicker1.date 
} 

使用此功能從變量

func load(){ 
    if date != nil { 
     datePicker1.setDate(date!, animated: true) 
    } 
}