2016-05-04 29 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

Mike、時間を節約する方法がわかりました。その時間を使ってアラートをトリガーすると、そのページがリロードされると、日付ピッカーが保存された時間をもう一度表示します。 –

0

あなたがUIDatePickerViewを使用している場合:(ストーリーボードで時刻に設定) 次の関数は、NSDateと時間をロード/保存されますと、あなたのUIDatePickerViewは時間に設定されているので、唯一の時間/分でしょうあなたは

変数にデータを保存するには、この機能を使って、あなたの選択した日付

var date :NSDate?  

を保存

変数を定義し、「時間」を表示させます

func save(){ 
    date = datePicker1.date 
} 

は、あなたの質問は、例えば(あなたが時間を節約したいどのような形式で指定されていない変数

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