How can I disable a navigation bar button when input on a ViewController is invalid?











up vote
0
down vote

favorite












I have a view controller, let's call it AddAlarmViewController.swift, which is inside a navigation controller, and in which I call



prepare(for segue: UIStoryboardSegue, sender: Any?)



This AddAlarmViewController comprises a screen in my iOS application that creates a new Alarm to add to a stack of Alarms back in a separate UIViewController based on input from some DatePickers.



Inside this prepare method, essentially before I return from this AddAlarmViewController to AlarmTableViewController, I use the following code



    // Configure the destination view controller only when the save button is pressed.
guard let button = sender as? UIBarButtonItem, button === saveButton else {
os_log("The cancel button was pressed, cancelling from AddAlarmViewController", log: OSLog.default, type: .debug)
return
}


After this, I pull the times from each datePicker into variables and use those variables to build an Alarm object.



The problem is that all of my logic for deciding whether a time is valid or not (end time is before start time, etc.) is contained in the Alarm object's init method and not in this AddAlarmViewController, so I can't seem to figure out how to change the navigation bar save button item to be disabled while invalid times are entered. Do I need to make the AddAlarmViewController a Delegate for the Alarm class or something?



Thanks










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have a view controller, let's call it AddAlarmViewController.swift, which is inside a navigation controller, and in which I call



    prepare(for segue: UIStoryboardSegue, sender: Any?)



    This AddAlarmViewController comprises a screen in my iOS application that creates a new Alarm to add to a stack of Alarms back in a separate UIViewController based on input from some DatePickers.



    Inside this prepare method, essentially before I return from this AddAlarmViewController to AlarmTableViewController, I use the following code



        // Configure the destination view controller only when the save button is pressed.
    guard let button = sender as? UIBarButtonItem, button === saveButton else {
    os_log("The cancel button was pressed, cancelling from AddAlarmViewController", log: OSLog.default, type: .debug)
    return
    }


    After this, I pull the times from each datePicker into variables and use those variables to build an Alarm object.



    The problem is that all of my logic for deciding whether a time is valid or not (end time is before start time, etc.) is contained in the Alarm object's init method and not in this AddAlarmViewController, so I can't seem to figure out how to change the navigation bar save button item to be disabled while invalid times are entered. Do I need to make the AddAlarmViewController a Delegate for the Alarm class or something?



    Thanks










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a view controller, let's call it AddAlarmViewController.swift, which is inside a navigation controller, and in which I call



      prepare(for segue: UIStoryboardSegue, sender: Any?)



      This AddAlarmViewController comprises a screen in my iOS application that creates a new Alarm to add to a stack of Alarms back in a separate UIViewController based on input from some DatePickers.



      Inside this prepare method, essentially before I return from this AddAlarmViewController to AlarmTableViewController, I use the following code



          // Configure the destination view controller only when the save button is pressed.
      guard let button = sender as? UIBarButtonItem, button === saveButton else {
      os_log("The cancel button was pressed, cancelling from AddAlarmViewController", log: OSLog.default, type: .debug)
      return
      }


      After this, I pull the times from each datePicker into variables and use those variables to build an Alarm object.



      The problem is that all of my logic for deciding whether a time is valid or not (end time is before start time, etc.) is contained in the Alarm object's init method and not in this AddAlarmViewController, so I can't seem to figure out how to change the navigation bar save button item to be disabled while invalid times are entered. Do I need to make the AddAlarmViewController a Delegate for the Alarm class or something?



      Thanks










      share|improve this question













      I have a view controller, let's call it AddAlarmViewController.swift, which is inside a navigation controller, and in which I call



      prepare(for segue: UIStoryboardSegue, sender: Any?)



      This AddAlarmViewController comprises a screen in my iOS application that creates a new Alarm to add to a stack of Alarms back in a separate UIViewController based on input from some DatePickers.



      Inside this prepare method, essentially before I return from this AddAlarmViewController to AlarmTableViewController, I use the following code



          // Configure the destination view controller only when the save button is pressed.
      guard let button = sender as? UIBarButtonItem, button === saveButton else {
      os_log("The cancel button was pressed, cancelling from AddAlarmViewController", log: OSLog.default, type: .debug)
      return
      }


      After this, I pull the times from each datePicker into variables and use those variables to build an Alarm object.



      The problem is that all of my logic for deciding whether a time is valid or not (end time is before start time, etc.) is contained in the Alarm object's init method and not in this AddAlarmViewController, so I can't seem to figure out how to change the navigation bar save button item to be disabled while invalid times are entered. Do I need to make the AddAlarmViewController a Delegate for the Alarm class or something?



      Thanks







      ios swift






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 2:09









      Tyler Cheek

      678




      678
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          My suggestion is put the logic into



            func shouldPerformSegue(withIdentifier identifier: NSStoryboardSegue.Identifier, sender: Any?) -> Bool


          That may help you solve the issue.



           extension : UIViewController{
          override func shouldPerformSegue(withIdentifier identifier: NSStoryboardSegue.Identifier, sender: Any?) -> Bool{
          return true


          }
          }






          share|improve this answer























          • This doesn't explain what file to put this method into, nor how it works :(. The Apple documentation on this method is dreadfully short.
            – Tyler Cheek
            Nov 22 at 2:44










          • You are suppose to override shouldPerformSegue in your presenting UIViewController or add an extension for all UIViewControllers. BTW, there are tons of examples here. Just search via SO.
            – E.Coms
            Nov 22 at 2:46












          • I guess what I'm confused about is how does the data from inside my Alarm object get back to my AddAlarmViewController? How does shouldPerformSeque() help me with that?
            – Tyler Cheek
            Nov 22 at 3:17











          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',
          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%2f53422950%2fhow-can-i-disable-a-navigation-bar-button-when-input-on-a-viewcontroller-is-inva%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








          up vote
          0
          down vote













          My suggestion is put the logic into



            func shouldPerformSegue(withIdentifier identifier: NSStoryboardSegue.Identifier, sender: Any?) -> Bool


          That may help you solve the issue.



           extension : UIViewController{
          override func shouldPerformSegue(withIdentifier identifier: NSStoryboardSegue.Identifier, sender: Any?) -> Bool{
          return true


          }
          }






          share|improve this answer























          • This doesn't explain what file to put this method into, nor how it works :(. The Apple documentation on this method is dreadfully short.
            – Tyler Cheek
            Nov 22 at 2:44










          • You are suppose to override shouldPerformSegue in your presenting UIViewController or add an extension for all UIViewControllers. BTW, there are tons of examples here. Just search via SO.
            – E.Coms
            Nov 22 at 2:46












          • I guess what I'm confused about is how does the data from inside my Alarm object get back to my AddAlarmViewController? How does shouldPerformSeque() help me with that?
            – Tyler Cheek
            Nov 22 at 3:17















          up vote
          0
          down vote













          My suggestion is put the logic into



            func shouldPerformSegue(withIdentifier identifier: NSStoryboardSegue.Identifier, sender: Any?) -> Bool


          That may help you solve the issue.



           extension : UIViewController{
          override func shouldPerformSegue(withIdentifier identifier: NSStoryboardSegue.Identifier, sender: Any?) -> Bool{
          return true


          }
          }






          share|improve this answer























          • This doesn't explain what file to put this method into, nor how it works :(. The Apple documentation on this method is dreadfully short.
            – Tyler Cheek
            Nov 22 at 2:44










          • You are suppose to override shouldPerformSegue in your presenting UIViewController or add an extension for all UIViewControllers. BTW, there are tons of examples here. Just search via SO.
            – E.Coms
            Nov 22 at 2:46












          • I guess what I'm confused about is how does the data from inside my Alarm object get back to my AddAlarmViewController? How does shouldPerformSeque() help me with that?
            – Tyler Cheek
            Nov 22 at 3:17













          up vote
          0
          down vote










          up vote
          0
          down vote









          My suggestion is put the logic into



            func shouldPerformSegue(withIdentifier identifier: NSStoryboardSegue.Identifier, sender: Any?) -> Bool


          That may help you solve the issue.



           extension : UIViewController{
          override func shouldPerformSegue(withIdentifier identifier: NSStoryboardSegue.Identifier, sender: Any?) -> Bool{
          return true


          }
          }






          share|improve this answer














          My suggestion is put the logic into



            func shouldPerformSegue(withIdentifier identifier: NSStoryboardSegue.Identifier, sender: Any?) -> Bool


          That may help you solve the issue.



           extension : UIViewController{
          override func shouldPerformSegue(withIdentifier identifier: NSStoryboardSegue.Identifier, sender: Any?) -> Bool{
          return true


          }
          }







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 at 2:45

























          answered Nov 22 at 2:34









          E.Coms

          1,4191412




          1,4191412












          • This doesn't explain what file to put this method into, nor how it works :(. The Apple documentation on this method is dreadfully short.
            – Tyler Cheek
            Nov 22 at 2:44










          • You are suppose to override shouldPerformSegue in your presenting UIViewController or add an extension for all UIViewControllers. BTW, there are tons of examples here. Just search via SO.
            – E.Coms
            Nov 22 at 2:46












          • I guess what I'm confused about is how does the data from inside my Alarm object get back to my AddAlarmViewController? How does shouldPerformSeque() help me with that?
            – Tyler Cheek
            Nov 22 at 3:17


















          • This doesn't explain what file to put this method into, nor how it works :(. The Apple documentation on this method is dreadfully short.
            – Tyler Cheek
            Nov 22 at 2:44










          • You are suppose to override shouldPerformSegue in your presenting UIViewController or add an extension for all UIViewControllers. BTW, there are tons of examples here. Just search via SO.
            – E.Coms
            Nov 22 at 2:46












          • I guess what I'm confused about is how does the data from inside my Alarm object get back to my AddAlarmViewController? How does shouldPerformSeque() help me with that?
            – Tyler Cheek
            Nov 22 at 3:17
















          This doesn't explain what file to put this method into, nor how it works :(. The Apple documentation on this method is dreadfully short.
          – Tyler Cheek
          Nov 22 at 2:44




          This doesn't explain what file to put this method into, nor how it works :(. The Apple documentation on this method is dreadfully short.
          – Tyler Cheek
          Nov 22 at 2:44












          You are suppose to override shouldPerformSegue in your presenting UIViewController or add an extension for all UIViewControllers. BTW, there are tons of examples here. Just search via SO.
          – E.Coms
          Nov 22 at 2:46






          You are suppose to override shouldPerformSegue in your presenting UIViewController or add an extension for all UIViewControllers. BTW, there are tons of examples here. Just search via SO.
          – E.Coms
          Nov 22 at 2:46














          I guess what I'm confused about is how does the data from inside my Alarm object get back to my AddAlarmViewController? How does shouldPerformSeque() help me with that?
          – Tyler Cheek
          Nov 22 at 3:17




          I guess what I'm confused about is how does the data from inside my Alarm object get back to my AddAlarmViewController? How does shouldPerformSeque() help me with that?
          – Tyler Cheek
          Nov 22 at 3:17


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53422950%2fhow-can-i-disable-a-navigation-bar-button-when-input-on-a-viewcontroller-is-inva%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

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)