Programmatically Pushing a ViewController in Swift











up vote
1
down vote

favorite












I'm trying to push a ViewController programmatically.



Code:



var plus = UIButton()

plus.addTarget(self, action: #selector(plusPressed), for: .touchUpInside)

@objc func plusPressed() {
print("plus")
let createJournalVC = CreateJournalViewController()
self.navigationController?.pushViewController(createJournalVC, animated: true)
}


What works:




  1. Once the button is pressed, "plus" is printed to the console.


What doesn't work:




  1. The ViewController is not pushed.


Details




  • I am using a Navigation Controller & Tab Bar Controller.

  • I am making this only programmatic, no storyboards.

  • There is no error printed to the console, nothing actually happens.










share|improve this question


















  • 1




    see this for help : stackoverflow.com/questions/39929592/…
    – Anbu.karthik
    22 hours ago










  • Check if the navigationController exists in the plusPressed method.
    – Rakesha Shastri
    22 hours ago










  • @Anbu.karthik But how come I don't have this issue in any of my other projects?
    – Version Swift
    22 hours ago










  • @RakeshaShastri expand please, sorry, I'm new.
    – Version Swift
    22 hours ago










  • set a breakpoint on the line where you push the viewController and when the execution stops there, check the value of navigationController
    – Rakesha Shastri
    22 hours ago















up vote
1
down vote

favorite












I'm trying to push a ViewController programmatically.



Code:



var plus = UIButton()

plus.addTarget(self, action: #selector(plusPressed), for: .touchUpInside)

@objc func plusPressed() {
print("plus")
let createJournalVC = CreateJournalViewController()
self.navigationController?.pushViewController(createJournalVC, animated: true)
}


What works:




  1. Once the button is pressed, "plus" is printed to the console.


What doesn't work:




  1. The ViewController is not pushed.


Details




  • I am using a Navigation Controller & Tab Bar Controller.

  • I am making this only programmatic, no storyboards.

  • There is no error printed to the console, nothing actually happens.










share|improve this question


















  • 1




    see this for help : stackoverflow.com/questions/39929592/…
    – Anbu.karthik
    22 hours ago










  • Check if the navigationController exists in the plusPressed method.
    – Rakesha Shastri
    22 hours ago










  • @Anbu.karthik But how come I don't have this issue in any of my other projects?
    – Version Swift
    22 hours ago










  • @RakeshaShastri expand please, sorry, I'm new.
    – Version Swift
    22 hours ago










  • set a breakpoint on the line where you push the viewController and when the execution stops there, check the value of navigationController
    – Rakesha Shastri
    22 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm trying to push a ViewController programmatically.



Code:



var plus = UIButton()

plus.addTarget(self, action: #selector(plusPressed), for: .touchUpInside)

@objc func plusPressed() {
print("plus")
let createJournalVC = CreateJournalViewController()
self.navigationController?.pushViewController(createJournalVC, animated: true)
}


What works:




  1. Once the button is pressed, "plus" is printed to the console.


What doesn't work:




  1. The ViewController is not pushed.


Details




  • I am using a Navigation Controller & Tab Bar Controller.

  • I am making this only programmatic, no storyboards.

  • There is no error printed to the console, nothing actually happens.










share|improve this question













I'm trying to push a ViewController programmatically.



Code:



var plus = UIButton()

plus.addTarget(self, action: #selector(plusPressed), for: .touchUpInside)

@objc func plusPressed() {
print("plus")
let createJournalVC = CreateJournalViewController()
self.navigationController?.pushViewController(createJournalVC, animated: true)
}


What works:




  1. Once the button is pressed, "plus" is printed to the console.


What doesn't work:




  1. The ViewController is not pushed.


Details




  • I am using a Navigation Controller & Tab Bar Controller.

  • I am making this only programmatic, no storyboards.

  • There is no error printed to the console, nothing actually happens.







ios swift uiviewcontroller viewcontroller pushviewcontroller






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 22 hours ago









Version Swift

258




258








  • 1




    see this for help : stackoverflow.com/questions/39929592/…
    – Anbu.karthik
    22 hours ago










  • Check if the navigationController exists in the plusPressed method.
    – Rakesha Shastri
    22 hours ago










  • @Anbu.karthik But how come I don't have this issue in any of my other projects?
    – Version Swift
    22 hours ago










  • @RakeshaShastri expand please, sorry, I'm new.
    – Version Swift
    22 hours ago










  • set a breakpoint on the line where you push the viewController and when the execution stops there, check the value of navigationController
    – Rakesha Shastri
    22 hours ago














  • 1




    see this for help : stackoverflow.com/questions/39929592/…
    – Anbu.karthik
    22 hours ago










  • Check if the navigationController exists in the plusPressed method.
    – Rakesha Shastri
    22 hours ago










  • @Anbu.karthik But how come I don't have this issue in any of my other projects?
    – Version Swift
    22 hours ago










  • @RakeshaShastri expand please, sorry, I'm new.
    – Version Swift
    22 hours ago










  • set a breakpoint on the line where you push the viewController and when the execution stops there, check the value of navigationController
    – Rakesha Shastri
    22 hours ago








1




1




see this for help : stackoverflow.com/questions/39929592/…
– Anbu.karthik
22 hours ago




see this for help : stackoverflow.com/questions/39929592/…
– Anbu.karthik
22 hours ago












Check if the navigationController exists in the plusPressed method.
– Rakesha Shastri
22 hours ago




Check if the navigationController exists in the plusPressed method.
– Rakesha Shastri
22 hours ago












@Anbu.karthik But how come I don't have this issue in any of my other projects?
– Version Swift
22 hours ago




@Anbu.karthik But how come I don't have this issue in any of my other projects?
– Version Swift
22 hours ago












@RakeshaShastri expand please, sorry, I'm new.
– Version Swift
22 hours ago




@RakeshaShastri expand please, sorry, I'm new.
– Version Swift
22 hours ago












set a breakpoint on the line where you push the viewController and when the execution stops there, check the value of navigationController
– Rakesha Shastri
22 hours ago




set a breakpoint on the line where you push the viewController and when the execution stops there, check the value of navigationController
– Rakesha Shastri
22 hours ago












3 Answers
3






active

oldest

votes

















up vote
0
down vote













in the navigation stack after the navigation your UItabbar will come then UItabbar will active as Root controller so in here navigationcontroller can become nil. if you faced this scenario then you need to initilize the navigation controller progrmatically or embed with uitabbarcontroller



 let createJournalVC = CreateJournalViewController()
if let navigationController = self.navigationController { navigationController.pushViewController(createJournalVC, animated: true)
}else{
let navigationController = UINavigationController.init(rootViewController: createJournalVC)
self.navigationController?.pushViewController(createJournalVC, animated: true)
}





share|improve this answer






























    up vote
    0
    down vote













    If TabBarController comes after NavigationController then NavigationController can become nil. You should rather put TabBarController first and then put each ViewController (that are related to each tab) into there own NavigationController.



    Storyboard:



    Each Tab's ViewController gets their own NavigationController



    Programmatically:



    You need to create your TabBarController like this...



    let tabCon = UITabBarController()
    let navCon1 = UINavigationController(rootViewController: CreateJournalViewController())
    let navCon2 = UINavigationController(rootViewController: ViewController())
    let navCon3 = UINavigationController(rootViewController: AnotherViewController())
    tabCon.viewControllers = [navCon1, navCon2, navCon3]





    share|improve this answer






























      up vote
      0
      down vote













      It seems navigationController is nil, that's why the view controller is not pushed.






      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',
        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%2f53371608%2fprogrammatically-pushing-a-viewcontroller-in-swift%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        0
        down vote













        in the navigation stack after the navigation your UItabbar will come then UItabbar will active as Root controller so in here navigationcontroller can become nil. if you faced this scenario then you need to initilize the navigation controller progrmatically or embed with uitabbarcontroller



         let createJournalVC = CreateJournalViewController()
        if let navigationController = self.navigationController { navigationController.pushViewController(createJournalVC, animated: true)
        }else{
        let navigationController = UINavigationController.init(rootViewController: createJournalVC)
        self.navigationController?.pushViewController(createJournalVC, animated: true)
        }





        share|improve this answer



























          up vote
          0
          down vote













          in the navigation stack after the navigation your UItabbar will come then UItabbar will active as Root controller so in here navigationcontroller can become nil. if you faced this scenario then you need to initilize the navigation controller progrmatically or embed with uitabbarcontroller



           let createJournalVC = CreateJournalViewController()
          if let navigationController = self.navigationController { navigationController.pushViewController(createJournalVC, animated: true)
          }else{
          let navigationController = UINavigationController.init(rootViewController: createJournalVC)
          self.navigationController?.pushViewController(createJournalVC, animated: true)
          }





          share|improve this answer

























            up vote
            0
            down vote










            up vote
            0
            down vote









            in the navigation stack after the navigation your UItabbar will come then UItabbar will active as Root controller so in here navigationcontroller can become nil. if you faced this scenario then you need to initilize the navigation controller progrmatically or embed with uitabbarcontroller



             let createJournalVC = CreateJournalViewController()
            if let navigationController = self.navigationController { navigationController.pushViewController(createJournalVC, animated: true)
            }else{
            let navigationController = UINavigationController.init(rootViewController: createJournalVC)
            self.navigationController?.pushViewController(createJournalVC, animated: true)
            }





            share|improve this answer














            in the navigation stack after the navigation your UItabbar will come then UItabbar will active as Root controller so in here navigationcontroller can become nil. if you faced this scenario then you need to initilize the navigation controller progrmatically or embed with uitabbarcontroller



             let createJournalVC = CreateJournalViewController()
            if let navigationController = self.navigationController { navigationController.pushViewController(createJournalVC, animated: true)
            }else{
            let navigationController = UINavigationController.init(rootViewController: createJournalVC)
            self.navigationController?.pushViewController(createJournalVC, animated: true)
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 22 hours ago

























            answered 22 hours ago









            Anbu.karthik

            59.1k15121111




            59.1k15121111
























                up vote
                0
                down vote













                If TabBarController comes after NavigationController then NavigationController can become nil. You should rather put TabBarController first and then put each ViewController (that are related to each tab) into there own NavigationController.



                Storyboard:



                Each Tab's ViewController gets their own NavigationController



                Programmatically:



                You need to create your TabBarController like this...



                let tabCon = UITabBarController()
                let navCon1 = UINavigationController(rootViewController: CreateJournalViewController())
                let navCon2 = UINavigationController(rootViewController: ViewController())
                let navCon3 = UINavigationController(rootViewController: AnotherViewController())
                tabCon.viewControllers = [navCon1, navCon2, navCon3]





                share|improve this answer



























                  up vote
                  0
                  down vote













                  If TabBarController comes after NavigationController then NavigationController can become nil. You should rather put TabBarController first and then put each ViewController (that are related to each tab) into there own NavigationController.



                  Storyboard:



                  Each Tab's ViewController gets their own NavigationController



                  Programmatically:



                  You need to create your TabBarController like this...



                  let tabCon = UITabBarController()
                  let navCon1 = UINavigationController(rootViewController: CreateJournalViewController())
                  let navCon2 = UINavigationController(rootViewController: ViewController())
                  let navCon3 = UINavigationController(rootViewController: AnotherViewController())
                  tabCon.viewControllers = [navCon1, navCon2, navCon3]





                  share|improve this answer

























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    If TabBarController comes after NavigationController then NavigationController can become nil. You should rather put TabBarController first and then put each ViewController (that are related to each tab) into there own NavigationController.



                    Storyboard:



                    Each Tab's ViewController gets their own NavigationController



                    Programmatically:



                    You need to create your TabBarController like this...



                    let tabCon = UITabBarController()
                    let navCon1 = UINavigationController(rootViewController: CreateJournalViewController())
                    let navCon2 = UINavigationController(rootViewController: ViewController())
                    let navCon3 = UINavigationController(rootViewController: AnotherViewController())
                    tabCon.viewControllers = [navCon1, navCon2, navCon3]





                    share|improve this answer














                    If TabBarController comes after NavigationController then NavigationController can become nil. You should rather put TabBarController first and then put each ViewController (that are related to each tab) into there own NavigationController.



                    Storyboard:



                    Each Tab's ViewController gets their own NavigationController



                    Programmatically:



                    You need to create your TabBarController like this...



                    let tabCon = UITabBarController()
                    let navCon1 = UINavigationController(rootViewController: CreateJournalViewController())
                    let navCon2 = UINavigationController(rootViewController: ViewController())
                    let navCon3 = UINavigationController(rootViewController: AnotherViewController())
                    tabCon.viewControllers = [navCon1, navCon2, navCon3]






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 21 hours ago

























                    answered 22 hours ago









                    N4SK

                    126113




                    126113






















                        up vote
                        0
                        down vote













                        It seems navigationController is nil, that's why the view controller is not pushed.






                        share|improve this answer

























                          up vote
                          0
                          down vote













                          It seems navigationController is nil, that's why the view controller is not pushed.






                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            It seems navigationController is nil, that's why the view controller is not pushed.






                            share|improve this answer












                            It seems navigationController is nil, that's why the view controller is not pushed.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 19 hours ago









                            Vladimir Kaltyrin

                            335112




                            335112






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371608%2fprogrammatically-pushing-a-viewcontroller-in-swift%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

                                Wiesbaden

                                Marschland

                                Dieringhausen