Save a selection from an UIDatePicker












1















So I have a UIDatePicker in Xcode. I need a way so that the UIdatePicker is saved automatically. So that the next time that user opens the app the UIDatePicker displays the same date that was inputed before he closed it last time. Here is some of my code



@IBOutlet weak var BirthdayPicker: UIDatePicker!
...
override func viewDidLoad() {
super.viewDidLoad()

//__________________________________________________________________________
// This makes sure the user doesnt use a date after the current date
var components = DateComponents()
components.year = -300

let minDate = Calendar.current.date(byAdding: components, to: Date())

components.year = 0
components.day = 0
components.hour = 0
components.minute = 0
components.second = 0
let maxDate = Calendar.current.date(byAdding: components, to: Date())

BirthdayPicker.minimumDate = minDate
BirthdayPicker.maximumDate = maxDate
//____________________________________


So whenever I open my app I get todays date. here's the screenshot This is the todays date



But what I really want when I open the app is



This is a random date that I just set it to



If you want me to be more specific or have any questions for me just leave a comment. I'm also not really sure of my code is useful but it’s something. Thank you.










share|improve this question





























    1















    So I have a UIDatePicker in Xcode. I need a way so that the UIdatePicker is saved automatically. So that the next time that user opens the app the UIDatePicker displays the same date that was inputed before he closed it last time. Here is some of my code



    @IBOutlet weak var BirthdayPicker: UIDatePicker!
    ...
    override func viewDidLoad() {
    super.viewDidLoad()

    //__________________________________________________________________________
    // This makes sure the user doesnt use a date after the current date
    var components = DateComponents()
    components.year = -300

    let minDate = Calendar.current.date(byAdding: components, to: Date())

    components.year = 0
    components.day = 0
    components.hour = 0
    components.minute = 0
    components.second = 0
    let maxDate = Calendar.current.date(byAdding: components, to: Date())

    BirthdayPicker.minimumDate = minDate
    BirthdayPicker.maximumDate = maxDate
    //____________________________________


    So whenever I open my app I get todays date. here's the screenshot This is the todays date



    But what I really want when I open the app is



    This is a random date that I just set it to



    If you want me to be more specific or have any questions for me just leave a comment. I'm also not really sure of my code is useful but it’s something. Thank you.










    share|improve this question



























      1












      1








      1








      So I have a UIDatePicker in Xcode. I need a way so that the UIdatePicker is saved automatically. So that the next time that user opens the app the UIDatePicker displays the same date that was inputed before he closed it last time. Here is some of my code



      @IBOutlet weak var BirthdayPicker: UIDatePicker!
      ...
      override func viewDidLoad() {
      super.viewDidLoad()

      //__________________________________________________________________________
      // This makes sure the user doesnt use a date after the current date
      var components = DateComponents()
      components.year = -300

      let minDate = Calendar.current.date(byAdding: components, to: Date())

      components.year = 0
      components.day = 0
      components.hour = 0
      components.minute = 0
      components.second = 0
      let maxDate = Calendar.current.date(byAdding: components, to: Date())

      BirthdayPicker.minimumDate = minDate
      BirthdayPicker.maximumDate = maxDate
      //____________________________________


      So whenever I open my app I get todays date. here's the screenshot This is the todays date



      But what I really want when I open the app is



      This is a random date that I just set it to



      If you want me to be more specific or have any questions for me just leave a comment. I'm also not really sure of my code is useful but it’s something. Thank you.










      share|improve this question
















      So I have a UIDatePicker in Xcode. I need a way so that the UIdatePicker is saved automatically. So that the next time that user opens the app the UIDatePicker displays the same date that was inputed before he closed it last time. Here is some of my code



      @IBOutlet weak var BirthdayPicker: UIDatePicker!
      ...
      override func viewDidLoad() {
      super.viewDidLoad()

      //__________________________________________________________________________
      // This makes sure the user doesnt use a date after the current date
      var components = DateComponents()
      components.year = -300

      let minDate = Calendar.current.date(byAdding: components, to: Date())

      components.year = 0
      components.day = 0
      components.hour = 0
      components.minute = 0
      components.second = 0
      let maxDate = Calendar.current.date(byAdding: components, to: Date())

      BirthdayPicker.minimumDate = minDate
      BirthdayPicker.maximumDate = maxDate
      //____________________________________


      So whenever I open my app I get todays date. here's the screenshot This is the todays date



      But what I really want when I open the app is



      This is a random date that I just set it to



      If you want me to be more specific or have any questions for me just leave a comment. I'm also not really sure of my code is useful but it’s something. Thank you.







      swift uidatepicker saving-data






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 31 '18 at 11:56









      Callum

      347114




      347114










      asked Dec 29 '18 at 17:37









      Bhavin pBhavin p

      186




      186
























          1 Answer
          1






          active

          oldest

          votes


















          3














          UIDatePicker has property date which represents current Date selected in date picker. You goal should be saving this date somewhere. UserDefaults should be enough for this case.



          Start with creating IBAction for your date picker for event .valueChanged and when value is changed, save current date of your picker to UserDefaults.standard with key "BirthdayDate"



          @IBAction func datePickerChangedValue(_ sender: UIDatePicker) {
          UserDefaults.standard.set(sender.date, forKey: "BirthdayDate")
          }


          then in viewDidLoad if there is already saved date in for key BirthdayDate, set current date of BirthdayPicker



          if let date = UserDefaults.standard.object(forKey: "BirthdayDate") as? Date {
          BirthdayPicker.date = date
          }




          override func viewDidLoad() {
          super.viewDidLoad()
          ...
          var components = DateComponents()
          components.year = -300
          let minDate = Calendar.current.date(byAdding: components, to: Date())
          BirthdayPicker.minimumDate = minDate
          BirthdayPicker.maximumDate = Date() // if `maxDate` is just current date, you can use `Date()`

          if let date = UserDefaults.standard.object(forKey: "BirthdayDate") as? Date {
          BirthdayPicker.date = date
          }
          }

          @IBAction func datePickerChangedValue(_ sender: UIDatePicker) {
          UserDefaults.standard.set(sender.date, forKey: "BirthdayDate")
          }





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53971854%2fsave-a-selection-from-an-uidatepicker%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            UIDatePicker has property date which represents current Date selected in date picker. You goal should be saving this date somewhere. UserDefaults should be enough for this case.



            Start with creating IBAction for your date picker for event .valueChanged and when value is changed, save current date of your picker to UserDefaults.standard with key "BirthdayDate"



            @IBAction func datePickerChangedValue(_ sender: UIDatePicker) {
            UserDefaults.standard.set(sender.date, forKey: "BirthdayDate")
            }


            then in viewDidLoad if there is already saved date in for key BirthdayDate, set current date of BirthdayPicker



            if let date = UserDefaults.standard.object(forKey: "BirthdayDate") as? Date {
            BirthdayPicker.date = date
            }




            override func viewDidLoad() {
            super.viewDidLoad()
            ...
            var components = DateComponents()
            components.year = -300
            let minDate = Calendar.current.date(byAdding: components, to: Date())
            BirthdayPicker.minimumDate = minDate
            BirthdayPicker.maximumDate = Date() // if `maxDate` is just current date, you can use `Date()`

            if let date = UserDefaults.standard.object(forKey: "BirthdayDate") as? Date {
            BirthdayPicker.date = date
            }
            }

            @IBAction func datePickerChangedValue(_ sender: UIDatePicker) {
            UserDefaults.standard.set(sender.date, forKey: "BirthdayDate")
            }





            share|improve this answer






























              3














              UIDatePicker has property date which represents current Date selected in date picker. You goal should be saving this date somewhere. UserDefaults should be enough for this case.



              Start with creating IBAction for your date picker for event .valueChanged and when value is changed, save current date of your picker to UserDefaults.standard with key "BirthdayDate"



              @IBAction func datePickerChangedValue(_ sender: UIDatePicker) {
              UserDefaults.standard.set(sender.date, forKey: "BirthdayDate")
              }


              then in viewDidLoad if there is already saved date in for key BirthdayDate, set current date of BirthdayPicker



              if let date = UserDefaults.standard.object(forKey: "BirthdayDate") as? Date {
              BirthdayPicker.date = date
              }




              override func viewDidLoad() {
              super.viewDidLoad()
              ...
              var components = DateComponents()
              components.year = -300
              let minDate = Calendar.current.date(byAdding: components, to: Date())
              BirthdayPicker.minimumDate = minDate
              BirthdayPicker.maximumDate = Date() // if `maxDate` is just current date, you can use `Date()`

              if let date = UserDefaults.standard.object(forKey: "BirthdayDate") as? Date {
              BirthdayPicker.date = date
              }
              }

              @IBAction func datePickerChangedValue(_ sender: UIDatePicker) {
              UserDefaults.standard.set(sender.date, forKey: "BirthdayDate")
              }





              share|improve this answer




























                3












                3








                3







                UIDatePicker has property date which represents current Date selected in date picker. You goal should be saving this date somewhere. UserDefaults should be enough for this case.



                Start with creating IBAction for your date picker for event .valueChanged and when value is changed, save current date of your picker to UserDefaults.standard with key "BirthdayDate"



                @IBAction func datePickerChangedValue(_ sender: UIDatePicker) {
                UserDefaults.standard.set(sender.date, forKey: "BirthdayDate")
                }


                then in viewDidLoad if there is already saved date in for key BirthdayDate, set current date of BirthdayPicker



                if let date = UserDefaults.standard.object(forKey: "BirthdayDate") as? Date {
                BirthdayPicker.date = date
                }




                override func viewDidLoad() {
                super.viewDidLoad()
                ...
                var components = DateComponents()
                components.year = -300
                let minDate = Calendar.current.date(byAdding: components, to: Date())
                BirthdayPicker.minimumDate = minDate
                BirthdayPicker.maximumDate = Date() // if `maxDate` is just current date, you can use `Date()`

                if let date = UserDefaults.standard.object(forKey: "BirthdayDate") as? Date {
                BirthdayPicker.date = date
                }
                }

                @IBAction func datePickerChangedValue(_ sender: UIDatePicker) {
                UserDefaults.standard.set(sender.date, forKey: "BirthdayDate")
                }





                share|improve this answer















                UIDatePicker has property date which represents current Date selected in date picker. You goal should be saving this date somewhere. UserDefaults should be enough for this case.



                Start with creating IBAction for your date picker for event .valueChanged and when value is changed, save current date of your picker to UserDefaults.standard with key "BirthdayDate"



                @IBAction func datePickerChangedValue(_ sender: UIDatePicker) {
                UserDefaults.standard.set(sender.date, forKey: "BirthdayDate")
                }


                then in viewDidLoad if there is already saved date in for key BirthdayDate, set current date of BirthdayPicker



                if let date = UserDefaults.standard.object(forKey: "BirthdayDate") as? Date {
                BirthdayPicker.date = date
                }




                override func viewDidLoad() {
                super.viewDidLoad()
                ...
                var components = DateComponents()
                components.year = -300
                let minDate = Calendar.current.date(byAdding: components, to: Date())
                BirthdayPicker.minimumDate = minDate
                BirthdayPicker.maximumDate = Date() // if `maxDate` is just current date, you can use `Date()`

                if let date = UserDefaults.standard.object(forKey: "BirthdayDate") as? Date {
                BirthdayPicker.date = date
                }
                }

                @IBAction func datePickerChangedValue(_ sender: UIDatePicker) {
                UserDefaults.standard.set(sender.date, forKey: "BirthdayDate")
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 29 '18 at 18:47

























                answered Dec 29 '18 at 17:49









                Robert DreslerRobert Dresler

                5,6681526




                5,6681526






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53971854%2fsave-a-selection-from-an-uidatepicker%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Mossoró

                    Error while reading .h5 file using the rhdf5 package in R

                    Pushsharp Apns notification error: 'InvalidToken'