Call different viewControllers on click of tab bar of UITabBarController












0















Below is the image of my UITabBarController structure in storyboard.



enter image description here



Right now in storyboard, AboutUsViewController(UIViewController) is bound with my tabBar button click event i.e. If I click on tabBar button, AboutUsViewController is opening but now my functionality is based on certain conditions. I want to call ContactRequstViewController instead of AboutUsViewController on same tabBar button click.



Following is my code to open ContactRequstViewController.



-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
if (tabBarController.selectedIndex == 2){
UIStoryboard *story = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil];
ContactRequstViewController *contactVC = [story instantiateViewControllerWithIdentifier:@"ContactUsView"];
[self.navigationController pushViewController:contactVC animated:YES];
}
}


After writing above code I am not able to load ContactRequestViewController.










share|improve this question

























  • As far as i can see, there is no navigation controller to the UITabbarController so the self.navigationController should actually be nil and the reason why nothing is being pushed. Kindly have a look.

    – Rikh
    Nov 14 '18 at 20:19
















0















Below is the image of my UITabBarController structure in storyboard.



enter image description here



Right now in storyboard, AboutUsViewController(UIViewController) is bound with my tabBar button click event i.e. If I click on tabBar button, AboutUsViewController is opening but now my functionality is based on certain conditions. I want to call ContactRequstViewController instead of AboutUsViewController on same tabBar button click.



Following is my code to open ContactRequstViewController.



-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
if (tabBarController.selectedIndex == 2){
UIStoryboard *story = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil];
ContactRequstViewController *contactVC = [story instantiateViewControllerWithIdentifier:@"ContactUsView"];
[self.navigationController pushViewController:contactVC animated:YES];
}
}


After writing above code I am not able to load ContactRequestViewController.










share|improve this question

























  • As far as i can see, there is no navigation controller to the UITabbarController so the self.navigationController should actually be nil and the reason why nothing is being pushed. Kindly have a look.

    – Rikh
    Nov 14 '18 at 20:19














0












0








0








Below is the image of my UITabBarController structure in storyboard.



enter image description here



Right now in storyboard, AboutUsViewController(UIViewController) is bound with my tabBar button click event i.e. If I click on tabBar button, AboutUsViewController is opening but now my functionality is based on certain conditions. I want to call ContactRequstViewController instead of AboutUsViewController on same tabBar button click.



Following is my code to open ContactRequstViewController.



-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
if (tabBarController.selectedIndex == 2){
UIStoryboard *story = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil];
ContactRequstViewController *contactVC = [story instantiateViewControllerWithIdentifier:@"ContactUsView"];
[self.navigationController pushViewController:contactVC animated:YES];
}
}


After writing above code I am not able to load ContactRequestViewController.










share|improve this question
















Below is the image of my UITabBarController structure in storyboard.



enter image description here



Right now in storyboard, AboutUsViewController(UIViewController) is bound with my tabBar button click event i.e. If I click on tabBar button, AboutUsViewController is opening but now my functionality is based on certain conditions. I want to call ContactRequstViewController instead of AboutUsViewController on same tabBar button click.



Following is my code to open ContactRequstViewController.



-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
if (tabBarController.selectedIndex == 2){
UIStoryboard *story = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil];
ContactRequstViewController *contactVC = [story instantiateViewControllerWithIdentifier:@"ContactUsView"];
[self.navigationController pushViewController:contactVC animated:YES];
}
}


After writing above code I am not able to load ContactRequestViewController.







ios objective-c uitabbarcontroller uitabbar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 17:37









kit

1,1063816




1,1063816










asked Nov 14 '18 at 17:16









iPhoneiPhone

2,14832651




2,14832651













  • As far as i can see, there is no navigation controller to the UITabbarController so the self.navigationController should actually be nil and the reason why nothing is being pushed. Kindly have a look.

    – Rikh
    Nov 14 '18 at 20:19



















  • As far as i can see, there is no navigation controller to the UITabbarController so the self.navigationController should actually be nil and the reason why nothing is being pushed. Kindly have a look.

    – Rikh
    Nov 14 '18 at 20:19

















As far as i can see, there is no navigation controller to the UITabbarController so the self.navigationController should actually be nil and the reason why nothing is being pushed. Kindly have a look.

– Rikh
Nov 14 '18 at 20:19





As far as i can see, there is no navigation controller to the UITabbarController so the self.navigationController should actually be nil and the reason why nothing is being pushed. Kindly have a look.

– Rikh
Nov 14 '18 at 20:19












2 Answers
2






active

oldest

votes


















0














If you want to replace the root view controller at that selected tab based on custom logic, try using setViewControllers:animated: method of the UITabBarController.



You could do something like this:



- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == 1 && tabBarController.viewControllers.count > tabBarController.selectedIndex) {
BOOL shouldShowContactVC = (BOOL)(rand() % 2);
NSMutableArray *viewControllers = [[tabBarController viewControllers] mutableCopy];
UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *newVC = nil;
if (shouldShowContactVC) {
newVC = [main instantiateViewControllerWithIdentifier:@"ContactUsVC"];
} else {
newVC = [main instantiateViewControllerWithIdentifier:@"AboutUsVC"];
}
if (newVC) {
[viewControllers replaceObjectAtIndex:tabBarController.selectedIndex withObject:newVC];
newVC.tabBarItem = viewController.tabBarItem;
[tabBarController setViewControllers:viewControllers animated:YES];
}
}
}





share|improve this answer


























  • I implemented your code but I am not able to load ContactRequestViewController.

    – iPhone
    Nov 14 '18 at 17:44











  • @iPhone Okay, I created an example project. You can find it here: bitbucket.org/pranay_k/tabbarexample/commits/all. Check it out and let me know.

    – Pranay
    Nov 14 '18 at 18:20











  • I implemented this too but did not get success. Let me clear one thing my code is not written in appdelegate.m file but it is in MainTabController(UITabBarController) file which is bound to Main Tab Controller in storyboard.

    – iPhone
    Nov 14 '18 at 18:35











  • let me clear that my code is not written in appdelegate.m file but it is written in mainTabController(UITabController) which is bound to Main Tab Controller in storyboard file.

    – iPhone
    Nov 14 '18 at 18:51













  • It doesn't matter where you set the delegate. Be it inside the AppDelegate or anywhere else.

    – Pranay
    Nov 14 '18 at 19:04



















0














In my experience , sometimes we don't use tabbarVC, because it's not flexible, such as hide, show, or change items number. So we set a navigationVC as rootVC, u can create a View, just make it's look like a tabbarVC.
This view is more easy to used.And u don't need to care any delegate method or something else. u can do anything u want.
That's all.






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%2f53305560%2fcall-different-viewcontrollers-on-click-of-tab-bar-of-uitabbarcontroller%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If you want to replace the root view controller at that selected tab based on custom logic, try using setViewControllers:animated: method of the UITabBarController.



    You could do something like this:



    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if (tabBarController.selectedIndex == 1 && tabBarController.viewControllers.count > tabBarController.selectedIndex) {
    BOOL shouldShowContactVC = (BOOL)(rand() % 2);
    NSMutableArray *viewControllers = [[tabBarController viewControllers] mutableCopy];
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *newVC = nil;
    if (shouldShowContactVC) {
    newVC = [main instantiateViewControllerWithIdentifier:@"ContactUsVC"];
    } else {
    newVC = [main instantiateViewControllerWithIdentifier:@"AboutUsVC"];
    }
    if (newVC) {
    [viewControllers replaceObjectAtIndex:tabBarController.selectedIndex withObject:newVC];
    newVC.tabBarItem = viewController.tabBarItem;
    [tabBarController setViewControllers:viewControllers animated:YES];
    }
    }
    }





    share|improve this answer


























    • I implemented your code but I am not able to load ContactRequestViewController.

      – iPhone
      Nov 14 '18 at 17:44











    • @iPhone Okay, I created an example project. You can find it here: bitbucket.org/pranay_k/tabbarexample/commits/all. Check it out and let me know.

      – Pranay
      Nov 14 '18 at 18:20











    • I implemented this too but did not get success. Let me clear one thing my code is not written in appdelegate.m file but it is in MainTabController(UITabBarController) file which is bound to Main Tab Controller in storyboard.

      – iPhone
      Nov 14 '18 at 18:35











    • let me clear that my code is not written in appdelegate.m file but it is written in mainTabController(UITabController) which is bound to Main Tab Controller in storyboard file.

      – iPhone
      Nov 14 '18 at 18:51













    • It doesn't matter where you set the delegate. Be it inside the AppDelegate or anywhere else.

      – Pranay
      Nov 14 '18 at 19:04
















    0














    If you want to replace the root view controller at that selected tab based on custom logic, try using setViewControllers:animated: method of the UITabBarController.



    You could do something like this:



    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if (tabBarController.selectedIndex == 1 && tabBarController.viewControllers.count > tabBarController.selectedIndex) {
    BOOL shouldShowContactVC = (BOOL)(rand() % 2);
    NSMutableArray *viewControllers = [[tabBarController viewControllers] mutableCopy];
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *newVC = nil;
    if (shouldShowContactVC) {
    newVC = [main instantiateViewControllerWithIdentifier:@"ContactUsVC"];
    } else {
    newVC = [main instantiateViewControllerWithIdentifier:@"AboutUsVC"];
    }
    if (newVC) {
    [viewControllers replaceObjectAtIndex:tabBarController.selectedIndex withObject:newVC];
    newVC.tabBarItem = viewController.tabBarItem;
    [tabBarController setViewControllers:viewControllers animated:YES];
    }
    }
    }





    share|improve this answer


























    • I implemented your code but I am not able to load ContactRequestViewController.

      – iPhone
      Nov 14 '18 at 17:44











    • @iPhone Okay, I created an example project. You can find it here: bitbucket.org/pranay_k/tabbarexample/commits/all. Check it out and let me know.

      – Pranay
      Nov 14 '18 at 18:20











    • I implemented this too but did not get success. Let me clear one thing my code is not written in appdelegate.m file but it is in MainTabController(UITabBarController) file which is bound to Main Tab Controller in storyboard.

      – iPhone
      Nov 14 '18 at 18:35











    • let me clear that my code is not written in appdelegate.m file but it is written in mainTabController(UITabController) which is bound to Main Tab Controller in storyboard file.

      – iPhone
      Nov 14 '18 at 18:51













    • It doesn't matter where you set the delegate. Be it inside the AppDelegate or anywhere else.

      – Pranay
      Nov 14 '18 at 19:04














    0












    0








    0







    If you want to replace the root view controller at that selected tab based on custom logic, try using setViewControllers:animated: method of the UITabBarController.



    You could do something like this:



    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if (tabBarController.selectedIndex == 1 && tabBarController.viewControllers.count > tabBarController.selectedIndex) {
    BOOL shouldShowContactVC = (BOOL)(rand() % 2);
    NSMutableArray *viewControllers = [[tabBarController viewControllers] mutableCopy];
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *newVC = nil;
    if (shouldShowContactVC) {
    newVC = [main instantiateViewControllerWithIdentifier:@"ContactUsVC"];
    } else {
    newVC = [main instantiateViewControllerWithIdentifier:@"AboutUsVC"];
    }
    if (newVC) {
    [viewControllers replaceObjectAtIndex:tabBarController.selectedIndex withObject:newVC];
    newVC.tabBarItem = viewController.tabBarItem;
    [tabBarController setViewControllers:viewControllers animated:YES];
    }
    }
    }





    share|improve this answer















    If you want to replace the root view controller at that selected tab based on custom logic, try using setViewControllers:animated: method of the UITabBarController.



    You could do something like this:



    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if (tabBarController.selectedIndex == 1 && tabBarController.viewControllers.count > tabBarController.selectedIndex) {
    BOOL shouldShowContactVC = (BOOL)(rand() % 2);
    NSMutableArray *viewControllers = [[tabBarController viewControllers] mutableCopy];
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *newVC = nil;
    if (shouldShowContactVC) {
    newVC = [main instantiateViewControllerWithIdentifier:@"ContactUsVC"];
    } else {
    newVC = [main instantiateViewControllerWithIdentifier:@"AboutUsVC"];
    }
    if (newVC) {
    [viewControllers replaceObjectAtIndex:tabBarController.selectedIndex withObject:newVC];
    newVC.tabBarItem = viewController.tabBarItem;
    [tabBarController setViewControllers:viewControllers animated:YES];
    }
    }
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 18:13

























    answered Nov 14 '18 at 17:38









    PranayPranay

    53616




    53616













    • I implemented your code but I am not able to load ContactRequestViewController.

      – iPhone
      Nov 14 '18 at 17:44











    • @iPhone Okay, I created an example project. You can find it here: bitbucket.org/pranay_k/tabbarexample/commits/all. Check it out and let me know.

      – Pranay
      Nov 14 '18 at 18:20











    • I implemented this too but did not get success. Let me clear one thing my code is not written in appdelegate.m file but it is in MainTabController(UITabBarController) file which is bound to Main Tab Controller in storyboard.

      – iPhone
      Nov 14 '18 at 18:35











    • let me clear that my code is not written in appdelegate.m file but it is written in mainTabController(UITabController) which is bound to Main Tab Controller in storyboard file.

      – iPhone
      Nov 14 '18 at 18:51













    • It doesn't matter where you set the delegate. Be it inside the AppDelegate or anywhere else.

      – Pranay
      Nov 14 '18 at 19:04



















    • I implemented your code but I am not able to load ContactRequestViewController.

      – iPhone
      Nov 14 '18 at 17:44











    • @iPhone Okay, I created an example project. You can find it here: bitbucket.org/pranay_k/tabbarexample/commits/all. Check it out and let me know.

      – Pranay
      Nov 14 '18 at 18:20











    • I implemented this too but did not get success. Let me clear one thing my code is not written in appdelegate.m file but it is in MainTabController(UITabBarController) file which is bound to Main Tab Controller in storyboard.

      – iPhone
      Nov 14 '18 at 18:35











    • let me clear that my code is not written in appdelegate.m file but it is written in mainTabController(UITabController) which is bound to Main Tab Controller in storyboard file.

      – iPhone
      Nov 14 '18 at 18:51













    • It doesn't matter where you set the delegate. Be it inside the AppDelegate or anywhere else.

      – Pranay
      Nov 14 '18 at 19:04

















    I implemented your code but I am not able to load ContactRequestViewController.

    – iPhone
    Nov 14 '18 at 17:44





    I implemented your code but I am not able to load ContactRequestViewController.

    – iPhone
    Nov 14 '18 at 17:44













    @iPhone Okay, I created an example project. You can find it here: bitbucket.org/pranay_k/tabbarexample/commits/all. Check it out and let me know.

    – Pranay
    Nov 14 '18 at 18:20





    @iPhone Okay, I created an example project. You can find it here: bitbucket.org/pranay_k/tabbarexample/commits/all. Check it out and let me know.

    – Pranay
    Nov 14 '18 at 18:20













    I implemented this too but did not get success. Let me clear one thing my code is not written in appdelegate.m file but it is in MainTabController(UITabBarController) file which is bound to Main Tab Controller in storyboard.

    – iPhone
    Nov 14 '18 at 18:35





    I implemented this too but did not get success. Let me clear one thing my code is not written in appdelegate.m file but it is in MainTabController(UITabBarController) file which is bound to Main Tab Controller in storyboard.

    – iPhone
    Nov 14 '18 at 18:35













    let me clear that my code is not written in appdelegate.m file but it is written in mainTabController(UITabController) which is bound to Main Tab Controller in storyboard file.

    – iPhone
    Nov 14 '18 at 18:51







    let me clear that my code is not written in appdelegate.m file but it is written in mainTabController(UITabController) which is bound to Main Tab Controller in storyboard file.

    – iPhone
    Nov 14 '18 at 18:51















    It doesn't matter where you set the delegate. Be it inside the AppDelegate or anywhere else.

    – Pranay
    Nov 14 '18 at 19:04





    It doesn't matter where you set the delegate. Be it inside the AppDelegate or anywhere else.

    – Pranay
    Nov 14 '18 at 19:04













    0














    In my experience , sometimes we don't use tabbarVC, because it's not flexible, such as hide, show, or change items number. So we set a navigationVC as rootVC, u can create a View, just make it's look like a tabbarVC.
    This view is more easy to used.And u don't need to care any delegate method or something else. u can do anything u want.
    That's all.






    share|improve this answer




























      0














      In my experience , sometimes we don't use tabbarVC, because it's not flexible, such as hide, show, or change items number. So we set a navigationVC as rootVC, u can create a View, just make it's look like a tabbarVC.
      This view is more easy to used.And u don't need to care any delegate method or something else. u can do anything u want.
      That's all.






      share|improve this answer


























        0












        0








        0







        In my experience , sometimes we don't use tabbarVC, because it's not flexible, such as hide, show, or change items number. So we set a navigationVC as rootVC, u can create a View, just make it's look like a tabbarVC.
        This view is more easy to used.And u don't need to care any delegate method or something else. u can do anything u want.
        That's all.






        share|improve this answer













        In my experience , sometimes we don't use tabbarVC, because it's not flexible, such as hide, show, or change items number. So we set a navigationVC as rootVC, u can create a View, just make it's look like a tabbarVC.
        This view is more easy to used.And u don't need to care any delegate method or something else. u can do anything u want.
        That's all.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 2:17









        KirinzerKirinzer

        111




        111






























            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%2f53305560%2fcall-different-viewcontrollers-on-click-of-tab-bar-of-uitabbarcontroller%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

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values