Are there reasons to have global variables for storyboards in iOS











up vote
1
down vote

favorite
2












While looking at the code of various iOS apps posted online, I noticed 2 distinct patterns:





  1. Create a storyboard instance where you need it, instantiate view, don't preserve either. E.g.:



    func switchView() {
    let storyboard = UIStoryboard(name: "Storyboard1", bundle: nil)
    let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController1") as! ViewController1
    self.navigationController?.pushViewController(viewController, animated: true )
    }



  2. On opposite end are cases when people instantiate storyboard(s) early (e.g. as constants in AppDelegate) and refer to those global instances when they need a storyboard. Sometimes even some views are referred that way. E.g.:



    class AppDelegate: UIResponder, UIApplicationDelegate {

    static var shared: AppDelegate!
    let storyboard1 = UIStoryboard(name: "Storyboard1", bundle: nil)
    let storyboard2 = UIStoryboard(name: "Storyboard2", bundle: nil)
    // etc
    }


    and then from elsewhere



    let viewController = AppDelegate.shared.storyboard1.instantiateViewController(withIdentifier: "ViewController1")



It appears that they do it mostly for 3 reasons:




  • They think time is saved on loading storyboards every time, especially for larger storyboards

  • Potentially memory is saved since app doesn't need to hold multiple copies of the same storyboard in memory

  • They guarantee that obvious dev mistakes (e.g. wrong storyboard name) are caught early, and are not made in multiple places


The last one is sort of true, but same could be achieved by using constants for storyboard names. And I could not find any indication that load time / memory claims are actually true. So, other than personal subjective preference, what are the objective advantages of this second approach over the first?










share|improve this question


























    up vote
    1
    down vote

    favorite
    2












    While looking at the code of various iOS apps posted online, I noticed 2 distinct patterns:





    1. Create a storyboard instance where you need it, instantiate view, don't preserve either. E.g.:



      func switchView() {
      let storyboard = UIStoryboard(name: "Storyboard1", bundle: nil)
      let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController1") as! ViewController1
      self.navigationController?.pushViewController(viewController, animated: true )
      }



    2. On opposite end are cases when people instantiate storyboard(s) early (e.g. as constants in AppDelegate) and refer to those global instances when they need a storyboard. Sometimes even some views are referred that way. E.g.:



      class AppDelegate: UIResponder, UIApplicationDelegate {

      static var shared: AppDelegate!
      let storyboard1 = UIStoryboard(name: "Storyboard1", bundle: nil)
      let storyboard2 = UIStoryboard(name: "Storyboard2", bundle: nil)
      // etc
      }


      and then from elsewhere



      let viewController = AppDelegate.shared.storyboard1.instantiateViewController(withIdentifier: "ViewController1")



    It appears that they do it mostly for 3 reasons:




    • They think time is saved on loading storyboards every time, especially for larger storyboards

    • Potentially memory is saved since app doesn't need to hold multiple copies of the same storyboard in memory

    • They guarantee that obvious dev mistakes (e.g. wrong storyboard name) are caught early, and are not made in multiple places


    The last one is sort of true, but same could be achieved by using constants for storyboard names. And I could not find any indication that load time / memory claims are actually true. So, other than personal subjective preference, what are the objective advantages of this second approach over the first?










    share|improve this question
























      up vote
      1
      down vote

      favorite
      2









      up vote
      1
      down vote

      favorite
      2






      2





      While looking at the code of various iOS apps posted online, I noticed 2 distinct patterns:





      1. Create a storyboard instance where you need it, instantiate view, don't preserve either. E.g.:



        func switchView() {
        let storyboard = UIStoryboard(name: "Storyboard1", bundle: nil)
        let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController1") as! ViewController1
        self.navigationController?.pushViewController(viewController, animated: true )
        }



      2. On opposite end are cases when people instantiate storyboard(s) early (e.g. as constants in AppDelegate) and refer to those global instances when they need a storyboard. Sometimes even some views are referred that way. E.g.:



        class AppDelegate: UIResponder, UIApplicationDelegate {

        static var shared: AppDelegate!
        let storyboard1 = UIStoryboard(name: "Storyboard1", bundle: nil)
        let storyboard2 = UIStoryboard(name: "Storyboard2", bundle: nil)
        // etc
        }


        and then from elsewhere



        let viewController = AppDelegate.shared.storyboard1.instantiateViewController(withIdentifier: "ViewController1")



      It appears that they do it mostly for 3 reasons:




      • They think time is saved on loading storyboards every time, especially for larger storyboards

      • Potentially memory is saved since app doesn't need to hold multiple copies of the same storyboard in memory

      • They guarantee that obvious dev mistakes (e.g. wrong storyboard name) are caught early, and are not made in multiple places


      The last one is sort of true, but same could be achieved by using constants for storyboard names. And I could not find any indication that load time / memory claims are actually true. So, other than personal subjective preference, what are the objective advantages of this second approach over the first?










      share|improve this question













      While looking at the code of various iOS apps posted online, I noticed 2 distinct patterns:





      1. Create a storyboard instance where you need it, instantiate view, don't preserve either. E.g.:



        func switchView() {
        let storyboard = UIStoryboard(name: "Storyboard1", bundle: nil)
        let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController1") as! ViewController1
        self.navigationController?.pushViewController(viewController, animated: true )
        }



      2. On opposite end are cases when people instantiate storyboard(s) early (e.g. as constants in AppDelegate) and refer to those global instances when they need a storyboard. Sometimes even some views are referred that way. E.g.:



        class AppDelegate: UIResponder, UIApplicationDelegate {

        static var shared: AppDelegate!
        let storyboard1 = UIStoryboard(name: "Storyboard1", bundle: nil)
        let storyboard2 = UIStoryboard(name: "Storyboard2", bundle: nil)
        // etc
        }


        and then from elsewhere



        let viewController = AppDelegate.shared.storyboard1.instantiateViewController(withIdentifier: "ViewController1")



      It appears that they do it mostly for 3 reasons:




      • They think time is saved on loading storyboards every time, especially for larger storyboards

      • Potentially memory is saved since app doesn't need to hold multiple copies of the same storyboard in memory

      • They guarantee that obvious dev mistakes (e.g. wrong storyboard name) are caught early, and are not made in multiple places


      The last one is sort of true, but same could be achieved by using constants for storyboard names. And I could not find any indication that load time / memory claims are actually true. So, other than personal subjective preference, what are the objective advantages of this second approach over the first?







      ios swift






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 days ago









      Kiril S.

      5,33152041




      5,33152041





























          active

          oldest

          votes











          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%2f53402740%2fare-there-reasons-to-have-global-variables-for-storyboards-in-ios%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402740%2fare-there-reasons-to-have-global-variables-for-storyboards-in-ios%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

          Berounka

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

          Sphinx de Gizeh