Swift programmatically navigate to another view controller/scene












41















I'm using following code to programmatically navigate to another ViewController. It works fine, but it some how hides the navigation bar. How do I fix this? (the navigation bar is created by embeding the ViewController in the navigation controller if that matters.)



let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)









share|improve this question





























    41















    I'm using following code to programmatically navigate to another ViewController. It works fine, but it some how hides the navigation bar. How do I fix this? (the navigation bar is created by embeding the ViewController in the navigation controller if that matters.)



    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

    let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
    self.presentViewController(nextViewController, animated:true, completion:nil)









    share|improve this question



























      41












      41








      41


      18






      I'm using following code to programmatically navigate to another ViewController. It works fine, but it some how hides the navigation bar. How do I fix this? (the navigation bar is created by embeding the ViewController in the navigation controller if that matters.)



      let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

      let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
      self.presentViewController(nextViewController, animated:true, completion:nil)









      share|improve this question
















      I'm using following code to programmatically navigate to another ViewController. It works fine, but it some how hides the navigation bar. How do I fix this? (the navigation bar is created by embeding the ViewController in the navigation controller if that matters.)



      let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

      let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
      self.presentViewController(nextViewController, animated:true, completion:nil)






      ios swift uinavigationbar viewcontroller






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 12 '16 at 14:54









      rmaddy

      242k27316380




      242k27316380










      asked Sep 12 '16 at 12:17









      VictorVictor

      4601820




      4601820
























          6 Answers
          6






          active

          oldest

          votes


















          109














          In Swift 3



          With a programmatically created Controller



          If you want to navigate to Controller created Programmatically, then do this:



          let newViewController = NewViewController()
          self.navigationController?.pushViewController(newViewController, animated: true)


          With a StoryBoard created Controller



          If you want to navigate to Controller on StoryBoard with Identifier "newViewController", then do this:



          let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
          let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController
          self.present(newViewController, animated: true, completion: nil)





          share|improve this answer


























          • "as! NewViewController" is not needed in the storyboard option

            – Lavi Avigdor
            Jun 19 '17 at 9:38











          • yeh i know that is optional, but if we show then it becomes clear which viewcontroller is destination for another developer

            – jaiswal Rajan
            Jun 19 '17 at 10:21






          • 2





            Call storyBoard.instantiateViewController and self.present from the main thread, or you will have a delay in appearing viewController components

            – Alex
            Jul 25 '18 at 11:23











          • @Alex look, it is upto developer to call it from main thread, i have just written the answer of the question asked above.Your comment to call from main thread is not appropriate here.

            – jaiswal Rajan
            Aug 10 '18 at 6:03



















          11














          You should push the new viewcontroller by using current navigation controller, not present.



          self.navigationController.pushViewController(nextViewController, animated: true)





          share|improve this answer































            5














            SWIFT 4.x



            The Strings in double quotes always confuse me, so I think answer to this question needs some graphical presentation to clear this out.



            For a banking app, I have a LoginViewController and a BalanceViewController. Each have their respective screens.



            The app starts and shows the Login screen. When login is successful, app opens the Balance screen.



            Here is how it looks:



            enter image description here



            enter image description here



            The login success is handled like this:



            let storyBoard: UIStoryboard = UIStoryboard(name: "Balance", bundle: nil)
            let balanceViewController = storyBoard.instantiateViewController(withIdentifier: "balance") as! BalanceViewController
            self.present(balanceViewController, animated: true, completion: nil)


            As you can see, the storyboard ID 'balance' in small letters is what goes in the second line of the code, and this is the ID which is defined in the storyboard settings, as in the attached screenshot.



            The term 'Balance' with capital 'B' is the name of the storyboard file, which is used in the first line of the code.



            We know that using hard coded Strings in code is a very bad practice, but somehow in iOS development it has become a common practice, and Xcode doesn't even warn about them.






            share|improve this answer

































              3














              So If you present a view controller it will not show in navigation controller. It will just take complete screen. For this case you have to create another navigation controller and add your nextViewController as root for this and present this new navigationController.



              Another way is to just push the view controller.



              self.presentViewController(nextViewController, animated:true, completion:nil)


              For more info check Apple documentation:-
              https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/doc/uid/TP40006926-CH3-SW96






              share|improve this answer
























              • presentViewController has been renamed to present(loginController, animated:true, completion:nil)

                – kite_n_code
                Apr 7 '17 at 5:08





















              2














              According to @jaiswal Rajan in his answer. You can do a pushViewController like this:



              let storyBoard: UIStoryboard = UIStoryboard(name: "NewBotStoryboard", bundle: nil)
              let newViewController = storyBoard.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController
              self.navigationController?.pushViewController(newViewController, animated: true)





              share|improve this answer































                -2














                OperationQueue.main.addOperation {
                let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let newViewController = storyBoard.instantiateViewController(withIdentifier: "Storyboard ID") as! NewViewController
                self.present(newViewController, animated: true, completion: nil)
                }


                It worked for me when I put the code inside of the OperationQueue.main.addOperation, that will execute in the main thread for me.






                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%2f39450124%2fswift-programmatically-navigate-to-another-view-controller-scene%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  6 Answers
                  6






                  active

                  oldest

                  votes








                  6 Answers
                  6






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  109














                  In Swift 3



                  With a programmatically created Controller



                  If you want to navigate to Controller created Programmatically, then do this:



                  let newViewController = NewViewController()
                  self.navigationController?.pushViewController(newViewController, animated: true)


                  With a StoryBoard created Controller



                  If you want to navigate to Controller on StoryBoard with Identifier "newViewController", then do this:



                  let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                  let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController
                  self.present(newViewController, animated: true, completion: nil)





                  share|improve this answer


























                  • "as! NewViewController" is not needed in the storyboard option

                    – Lavi Avigdor
                    Jun 19 '17 at 9:38











                  • yeh i know that is optional, but if we show then it becomes clear which viewcontroller is destination for another developer

                    – jaiswal Rajan
                    Jun 19 '17 at 10:21






                  • 2





                    Call storyBoard.instantiateViewController and self.present from the main thread, or you will have a delay in appearing viewController components

                    – Alex
                    Jul 25 '18 at 11:23











                  • @Alex look, it is upto developer to call it from main thread, i have just written the answer of the question asked above.Your comment to call from main thread is not appropriate here.

                    – jaiswal Rajan
                    Aug 10 '18 at 6:03
















                  109














                  In Swift 3



                  With a programmatically created Controller



                  If you want to navigate to Controller created Programmatically, then do this:



                  let newViewController = NewViewController()
                  self.navigationController?.pushViewController(newViewController, animated: true)


                  With a StoryBoard created Controller



                  If you want to navigate to Controller on StoryBoard with Identifier "newViewController", then do this:



                  let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                  let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController
                  self.present(newViewController, animated: true, completion: nil)





                  share|improve this answer


























                  • "as! NewViewController" is not needed in the storyboard option

                    – Lavi Avigdor
                    Jun 19 '17 at 9:38











                  • yeh i know that is optional, but if we show then it becomes clear which viewcontroller is destination for another developer

                    – jaiswal Rajan
                    Jun 19 '17 at 10:21






                  • 2





                    Call storyBoard.instantiateViewController and self.present from the main thread, or you will have a delay in appearing viewController components

                    – Alex
                    Jul 25 '18 at 11:23











                  • @Alex look, it is upto developer to call it from main thread, i have just written the answer of the question asked above.Your comment to call from main thread is not appropriate here.

                    – jaiswal Rajan
                    Aug 10 '18 at 6:03














                  109












                  109








                  109







                  In Swift 3



                  With a programmatically created Controller



                  If you want to navigate to Controller created Programmatically, then do this:



                  let newViewController = NewViewController()
                  self.navigationController?.pushViewController(newViewController, animated: true)


                  With a StoryBoard created Controller



                  If you want to navigate to Controller on StoryBoard with Identifier "newViewController", then do this:



                  let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                  let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController
                  self.present(newViewController, animated: true, completion: nil)





                  share|improve this answer















                  In Swift 3



                  With a programmatically created Controller



                  If you want to navigate to Controller created Programmatically, then do this:



                  let newViewController = NewViewController()
                  self.navigationController?.pushViewController(newViewController, animated: true)


                  With a StoryBoard created Controller



                  If you want to navigate to Controller on StoryBoard with Identifier "newViewController", then do this:



                  let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                  let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController
                  self.present(newViewController, animated: true, completion: nil)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 10 '18 at 6:01

























                  answered Dec 12 '16 at 7:06









                  jaiswal Rajanjaiswal Rajan

                  2,61711715




                  2,61711715













                  • "as! NewViewController" is not needed in the storyboard option

                    – Lavi Avigdor
                    Jun 19 '17 at 9:38











                  • yeh i know that is optional, but if we show then it becomes clear which viewcontroller is destination for another developer

                    – jaiswal Rajan
                    Jun 19 '17 at 10:21






                  • 2





                    Call storyBoard.instantiateViewController and self.present from the main thread, or you will have a delay in appearing viewController components

                    – Alex
                    Jul 25 '18 at 11:23











                  • @Alex look, it is upto developer to call it from main thread, i have just written the answer of the question asked above.Your comment to call from main thread is not appropriate here.

                    – jaiswal Rajan
                    Aug 10 '18 at 6:03



















                  • "as! NewViewController" is not needed in the storyboard option

                    – Lavi Avigdor
                    Jun 19 '17 at 9:38











                  • yeh i know that is optional, but if we show then it becomes clear which viewcontroller is destination for another developer

                    – jaiswal Rajan
                    Jun 19 '17 at 10:21






                  • 2





                    Call storyBoard.instantiateViewController and self.present from the main thread, or you will have a delay in appearing viewController components

                    – Alex
                    Jul 25 '18 at 11:23











                  • @Alex look, it is upto developer to call it from main thread, i have just written the answer of the question asked above.Your comment to call from main thread is not appropriate here.

                    – jaiswal Rajan
                    Aug 10 '18 at 6:03

















                  "as! NewViewController" is not needed in the storyboard option

                  – Lavi Avigdor
                  Jun 19 '17 at 9:38





                  "as! NewViewController" is not needed in the storyboard option

                  – Lavi Avigdor
                  Jun 19 '17 at 9:38













                  yeh i know that is optional, but if we show then it becomes clear which viewcontroller is destination for another developer

                  – jaiswal Rajan
                  Jun 19 '17 at 10:21





                  yeh i know that is optional, but if we show then it becomes clear which viewcontroller is destination for another developer

                  – jaiswal Rajan
                  Jun 19 '17 at 10:21




                  2




                  2





                  Call storyBoard.instantiateViewController and self.present from the main thread, or you will have a delay in appearing viewController components

                  – Alex
                  Jul 25 '18 at 11:23





                  Call storyBoard.instantiateViewController and self.present from the main thread, or you will have a delay in appearing viewController components

                  – Alex
                  Jul 25 '18 at 11:23













                  @Alex look, it is upto developer to call it from main thread, i have just written the answer of the question asked above.Your comment to call from main thread is not appropriate here.

                  – jaiswal Rajan
                  Aug 10 '18 at 6:03





                  @Alex look, it is upto developer to call it from main thread, i have just written the answer of the question asked above.Your comment to call from main thread is not appropriate here.

                  – jaiswal Rajan
                  Aug 10 '18 at 6:03













                  11














                  You should push the new viewcontroller by using current navigation controller, not present.



                  self.navigationController.pushViewController(nextViewController, animated: true)





                  share|improve this answer




























                    11














                    You should push the new viewcontroller by using current navigation controller, not present.



                    self.navigationController.pushViewController(nextViewController, animated: true)





                    share|improve this answer


























                      11












                      11








                      11







                      You should push the new viewcontroller by using current navigation controller, not present.



                      self.navigationController.pushViewController(nextViewController, animated: true)





                      share|improve this answer













                      You should push the new viewcontroller by using current navigation controller, not present.



                      self.navigationController.pushViewController(nextViewController, animated: true)






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Sep 12 '16 at 12:26









                      ocanalocanal

                      9,2111556109




                      9,2111556109























                          5














                          SWIFT 4.x



                          The Strings in double quotes always confuse me, so I think answer to this question needs some graphical presentation to clear this out.



                          For a banking app, I have a LoginViewController and a BalanceViewController. Each have their respective screens.



                          The app starts and shows the Login screen. When login is successful, app opens the Balance screen.



                          Here is how it looks:



                          enter image description here



                          enter image description here



                          The login success is handled like this:



                          let storyBoard: UIStoryboard = UIStoryboard(name: "Balance", bundle: nil)
                          let balanceViewController = storyBoard.instantiateViewController(withIdentifier: "balance") as! BalanceViewController
                          self.present(balanceViewController, animated: true, completion: nil)


                          As you can see, the storyboard ID 'balance' in small letters is what goes in the second line of the code, and this is the ID which is defined in the storyboard settings, as in the attached screenshot.



                          The term 'Balance' with capital 'B' is the name of the storyboard file, which is used in the first line of the code.



                          We know that using hard coded Strings in code is a very bad practice, but somehow in iOS development it has become a common practice, and Xcode doesn't even warn about them.






                          share|improve this answer






























                            5














                            SWIFT 4.x



                            The Strings in double quotes always confuse me, so I think answer to this question needs some graphical presentation to clear this out.



                            For a banking app, I have a LoginViewController and a BalanceViewController. Each have their respective screens.



                            The app starts and shows the Login screen. When login is successful, app opens the Balance screen.



                            Here is how it looks:



                            enter image description here



                            enter image description here



                            The login success is handled like this:



                            let storyBoard: UIStoryboard = UIStoryboard(name: "Balance", bundle: nil)
                            let balanceViewController = storyBoard.instantiateViewController(withIdentifier: "balance") as! BalanceViewController
                            self.present(balanceViewController, animated: true, completion: nil)


                            As you can see, the storyboard ID 'balance' in small letters is what goes in the second line of the code, and this is the ID which is defined in the storyboard settings, as in the attached screenshot.



                            The term 'Balance' with capital 'B' is the name of the storyboard file, which is used in the first line of the code.



                            We know that using hard coded Strings in code is a very bad practice, but somehow in iOS development it has become a common practice, and Xcode doesn't even warn about them.






                            share|improve this answer




























                              5












                              5








                              5







                              SWIFT 4.x



                              The Strings in double quotes always confuse me, so I think answer to this question needs some graphical presentation to clear this out.



                              For a banking app, I have a LoginViewController and a BalanceViewController. Each have their respective screens.



                              The app starts and shows the Login screen. When login is successful, app opens the Balance screen.



                              Here is how it looks:



                              enter image description here



                              enter image description here



                              The login success is handled like this:



                              let storyBoard: UIStoryboard = UIStoryboard(name: "Balance", bundle: nil)
                              let balanceViewController = storyBoard.instantiateViewController(withIdentifier: "balance") as! BalanceViewController
                              self.present(balanceViewController, animated: true, completion: nil)


                              As you can see, the storyboard ID 'balance' in small letters is what goes in the second line of the code, and this is the ID which is defined in the storyboard settings, as in the attached screenshot.



                              The term 'Balance' with capital 'B' is the name of the storyboard file, which is used in the first line of the code.



                              We know that using hard coded Strings in code is a very bad practice, but somehow in iOS development it has become a common practice, and Xcode doesn't even warn about them.






                              share|improve this answer















                              SWIFT 4.x



                              The Strings in double quotes always confuse me, so I think answer to this question needs some graphical presentation to clear this out.



                              For a banking app, I have a LoginViewController and a BalanceViewController. Each have their respective screens.



                              The app starts and shows the Login screen. When login is successful, app opens the Balance screen.



                              Here is how it looks:



                              enter image description here



                              enter image description here



                              The login success is handled like this:



                              let storyBoard: UIStoryboard = UIStoryboard(name: "Balance", bundle: nil)
                              let balanceViewController = storyBoard.instantiateViewController(withIdentifier: "balance") as! BalanceViewController
                              self.present(balanceViewController, animated: true, completion: nil)


                              As you can see, the storyboard ID 'balance' in small letters is what goes in the second line of the code, and this is the ID which is defined in the storyboard settings, as in the attached screenshot.



                              The term 'Balance' with capital 'B' is the name of the storyboard file, which is used in the first line of the code.



                              We know that using hard coded Strings in code is a very bad practice, but somehow in iOS development it has become a common practice, and Xcode doesn't even warn about them.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Dec 3 '18 at 15:53

























                              answered Nov 12 '18 at 17:59









                              zeeshanzeeshan

                              2,56713044




                              2,56713044























                                  3














                                  So If you present a view controller it will not show in navigation controller. It will just take complete screen. For this case you have to create another navigation controller and add your nextViewController as root for this and present this new navigationController.



                                  Another way is to just push the view controller.



                                  self.presentViewController(nextViewController, animated:true, completion:nil)


                                  For more info check Apple documentation:-
                                  https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/doc/uid/TP40006926-CH3-SW96






                                  share|improve this answer
























                                  • presentViewController has been renamed to present(loginController, animated:true, completion:nil)

                                    – kite_n_code
                                    Apr 7 '17 at 5:08


















                                  3














                                  So If you present a view controller it will not show in navigation controller. It will just take complete screen. For this case you have to create another navigation controller and add your nextViewController as root for this and present this new navigationController.



                                  Another way is to just push the view controller.



                                  self.presentViewController(nextViewController, animated:true, completion:nil)


                                  For more info check Apple documentation:-
                                  https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/doc/uid/TP40006926-CH3-SW96






                                  share|improve this answer
























                                  • presentViewController has been renamed to present(loginController, animated:true, completion:nil)

                                    – kite_n_code
                                    Apr 7 '17 at 5:08
















                                  3












                                  3








                                  3







                                  So If you present a view controller it will not show in navigation controller. It will just take complete screen. For this case you have to create another navigation controller and add your nextViewController as root for this and present this new navigationController.



                                  Another way is to just push the view controller.



                                  self.presentViewController(nextViewController, animated:true, completion:nil)


                                  For more info check Apple documentation:-
                                  https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/doc/uid/TP40006926-CH3-SW96






                                  share|improve this answer













                                  So If you present a view controller it will not show in navigation controller. It will just take complete screen. For this case you have to create another navigation controller and add your nextViewController as root for this and present this new navigationController.



                                  Another way is to just push the view controller.



                                  self.presentViewController(nextViewController, animated:true, completion:nil)


                                  For more info check Apple documentation:-
                                  https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/doc/uid/TP40006926-CH3-SW96







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Sep 12 '16 at 12:27









                                  AksAks

                                  1,053921




                                  1,053921













                                  • presentViewController has been renamed to present(loginController, animated:true, completion:nil)

                                    – kite_n_code
                                    Apr 7 '17 at 5:08





















                                  • presentViewController has been renamed to present(loginController, animated:true, completion:nil)

                                    – kite_n_code
                                    Apr 7 '17 at 5:08



















                                  presentViewController has been renamed to present(loginController, animated:true, completion:nil)

                                  – kite_n_code
                                  Apr 7 '17 at 5:08







                                  presentViewController has been renamed to present(loginController, animated:true, completion:nil)

                                  – kite_n_code
                                  Apr 7 '17 at 5:08













                                  2














                                  According to @jaiswal Rajan in his answer. You can do a pushViewController like this:



                                  let storyBoard: UIStoryboard = UIStoryboard(name: "NewBotStoryboard", bundle: nil)
                                  let newViewController = storyBoard.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController
                                  self.navigationController?.pushViewController(newViewController, animated: true)





                                  share|improve this answer




























                                    2














                                    According to @jaiswal Rajan in his answer. You can do a pushViewController like this:



                                    let storyBoard: UIStoryboard = UIStoryboard(name: "NewBotStoryboard", bundle: nil)
                                    let newViewController = storyBoard.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController
                                    self.navigationController?.pushViewController(newViewController, animated: true)





                                    share|improve this answer


























                                      2












                                      2








                                      2







                                      According to @jaiswal Rajan in his answer. You can do a pushViewController like this:



                                      let storyBoard: UIStoryboard = UIStoryboard(name: "NewBotStoryboard", bundle: nil)
                                      let newViewController = storyBoard.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController
                                      self.navigationController?.pushViewController(newViewController, animated: true)





                                      share|improve this answer













                                      According to @jaiswal Rajan in his answer. You can do a pushViewController like this:



                                      let storyBoard: UIStoryboard = UIStoryboard(name: "NewBotStoryboard", bundle: nil)
                                      let newViewController = storyBoard.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController
                                      self.navigationController?.pushViewController(newViewController, animated: true)






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jul 11 '18 at 17:19









                                      LagMasterLagMaster

                                      967




                                      967























                                          -2














                                          OperationQueue.main.addOperation {
                                          let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                                          let newViewController = storyBoard.instantiateViewController(withIdentifier: "Storyboard ID") as! NewViewController
                                          self.present(newViewController, animated: true, completion: nil)
                                          }


                                          It worked for me when I put the code inside of the OperationQueue.main.addOperation, that will execute in the main thread for me.






                                          share|improve this answer






























                                            -2














                                            OperationQueue.main.addOperation {
                                            let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                                            let newViewController = storyBoard.instantiateViewController(withIdentifier: "Storyboard ID") as! NewViewController
                                            self.present(newViewController, animated: true, completion: nil)
                                            }


                                            It worked for me when I put the code inside of the OperationQueue.main.addOperation, that will execute in the main thread for me.






                                            share|improve this answer




























                                              -2












                                              -2








                                              -2







                                              OperationQueue.main.addOperation {
                                              let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                                              let newViewController = storyBoard.instantiateViewController(withIdentifier: "Storyboard ID") as! NewViewController
                                              self.present(newViewController, animated: true, completion: nil)
                                              }


                                              It worked for me when I put the code inside of the OperationQueue.main.addOperation, that will execute in the main thread for me.






                                              share|improve this answer















                                              OperationQueue.main.addOperation {
                                              let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                                              let newViewController = storyBoard.instantiateViewController(withIdentifier: "Storyboard ID") as! NewViewController
                                              self.present(newViewController, animated: true, completion: nil)
                                              }


                                              It worked for me when I put the code inside of the OperationQueue.main.addOperation, that will execute in the main thread for me.







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Nov 24 '18 at 8:29









                                              Badhan Ganesh

                                              2,60021228




                                              2,60021228










                                              answered Mar 8 '17 at 17:35









                                              Pedro BerbelPedro Berbel

                                              1




                                              1






























                                                  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%2f39450124%2fswift-programmatically-navigate-to-another-view-controller-scene%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

                                                  404 Error Contact Form 7 ajax form submitting

                                                  How to know if a Active Directory user can login interactively

                                                  Refactoring coordinates for Minecraft Pi buildings written in Python