0

I recently work on a tutorial for my app. I created the tutorial with this Tutorial:

http://www.appcoda.com/uipageviewcontroller-tutorial-intro/

I created a "button" which brings the user back to the rootViewController, in this case the TabBarController. Problem is: with this tutorial I made a extra storyboard for the tutorial. So how can I go back to the original rootViewController(TabBarController) with the button?

Code:

- (IBAction)start:(id)sender {
        UIViewController* backToRootViewController = [[UIViewController alloc] initWithNibName:@"TabBarController" bundle:[NSBundle mainBundle]];
        [self.view addSubview:backToRootViewController.view];
}

This does not work, too

- (IBAction)start:(id)sender {
           [self.navigationController popToRootViewControllerAnimated:YES];
    }

EDIT

To open the tutorial at the first start:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
BOOL isAccepted = [standardUserDefaults boolForKey:@"iHaveAcceptedTheTerms"];
if (!isAccepted) {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.viewController = [[APPViewController alloc] initWithNibName:@"APPViewController" bundle:nil];
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
}

APPViewController is the Tutorial

EDIT

After the help of johnnelm9r the current code is this:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"main" bundle: nil];

    IntroViewController *introViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"IntroViewController"];

        BOOL isAccepted = [standardUserDefaults boolForKey:@"iHaveAcceptedTheTerms"];
        if (!isAccepted) {
            [self.window.rootViewController presentViewController:introViewController animated:NO completion:nil];
        }

But now, sadly, the app crashed and the error is:

Application tried to present a nil modal view controller on target <UITabBarController: 0x175409d0>.'

and also a warning: Incompatible pointer type assigning to 'ViewController' from 'UIViewController' in AppDelegate

user3450607
  • 117
  • 1
  • 2
  • 7
  • Are you using a UINavigationController to get away from the UITabBarController? – Ryan Poolos Apr 29 '14 at 18:15
  • Yes. TabBarController to NavigationController to ViewController – user3450607 Apr 29 '14 at 18:21
  • How are you transferring to the new storyboard? – johnnelm9r Apr 29 '14 at 19:14
  • Do not do any code in your app delegate. By choosing an initial view controller in your storyboard you do not need any code in the delegate. You should be presenting the tutorial view controller from the viewDidAppear method in the first tab view controller. You should be setting your storyboard in your project settings. – johnnelm9r Apr 29 '14 at 21:53
  • I would highly suggest you make a new project with a navbar template and storyboard. Erase the first and second view controller. Add a navigation controller as a relationship segue then a new view controller as a root to the navigationcontroller. Copy your classes from the project you have now but don't touch the app delegate. Set the view controller to the appropriate class that is not your tutorial. Then in the viewDidAppear method (you will have to add it and don't forget to call super) add the code I suggested. At that point everything should work and you can go from there. – johnnelm9r Apr 29 '14 at 22:01
  • Crap, Don't forget to add another view controller to the storyboard that is not connected to anything and give it an identifier and use that identifier in the code I gave you. Also set that to your tutorial class and import that into your root view controller. That is important! :) – johnnelm9r Apr 29 '14 at 22:05

2 Answers2

0

*****UPDATE****

I uploaded a sample project to demonstrate what I mean. You can find it here: github link Good luck!

**** EDIT

After talking with you more I'd suggest trying to use something similar to this code in your viewDidAppear method of whatever view controller runs the first tab of your tabbarcontroller (obviously you want your own class name where I have IntroViewController):

BOOL didShowIntro = [[NSUserDefaults standardUserDefaults] boolForKey:@"showIntro"];

IntroViewController *introViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"IntroViewController"];

if (didShowIntro)
    {
    [self presentViewController:introViewController animated:NO completion:nil];
    }

and then just pop the presented controller from your button in the tutorial like so:

[self dismissViewControllerAnimated:NO completion:nil];

Just remember to set your user defaults to no when you press the button in your tutorial view controller. And make sure you are going from tabbarcontroller to navigationcontroller as a relationship segue and then to the view controller you want to show after the tutorial as a root. Just to be clear: there should be no connection to the tutorial view controller on your storyboard.

Just to be extra clear: your storyboard should have the navbarcontroller as your initial view controller connected to a navigationcontroller connected to a view controller and another view controller not connected to anything. :) And make sure you delete the code in the delegate.

johnnelm9r
  • 201
  • 2
  • 9
  • Hi thank you very much so far. i tried all of your codes, but none of them worked. I also add an NSLog to see if the button is really tapped and the code will be executed. The App does not crash. nothing happens... – user3450607 Apr 29 '14 at 19:35
  • Why are you using two storyboards? And how are you transitioning from the first storyboard to the second initially? – johnnelm9r Apr 29 '14 at 20:03
  • i don't use two storyboards. for the tutorial i have two .xib files – user3450607 Apr 29 '14 at 20:26
  • Have you thought about using storyboards? And shouldn't you be initializing your main controller as your tabbarcontroller rather than the tutorial controller? – johnnelm9r Apr 29 '14 at 20:47
  • As you say it i tried to add the views into ViewController in the Main storyboard and set the class to the ViewContoller-files, but it does not work. even if i delete the original .xib files, on my phone there is still the old .xib. i know it because i changed the arrangement of the content in the storyboard and on my iPhone these changes will not appear – user3450607 Apr 29 '14 at 20:55
  • My tabBarController is the initial viewController, but when you launch the app for the first time the If-loop sets the tutorial as the initialViewController. i deleted the code where i change the NSUserDefault value , so i don't have to delete the app every time i want to test it. – user3450607 Apr 29 '14 at 20:57
  • Did you shift+option+command+k to clean the build? And if you still have that code in the didfinishlaunching method you will keep overriding your storyboard, I believe. Also, try deleting your app from the device and the simulator (if you are using it) and rebuild to fully clean it. – johnnelm9r Apr 29 '14 at 20:58
  • You should try having the app transition to the tutorial view controller from your initial view controller and not your delegate. That will clear it up quickly. As a guide I'd try the updated version of the tutorial you were using at: http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/ :) – johnnelm9r Apr 29 '14 at 21:01
  • i cleaned the build, and now nothing is working anymore, which is good. The error is "Could not load NIB in bundle:"APPViewController" ". So i guess i have to change the if loop not to set the tutorial as the initial view. Just show the tutorial, right? – user3450607 Apr 29 '14 at 21:06
  • Awesome that you really want to help me, but there is an issue... See my EDIT – user3450607 Apr 29 '14 at 21:41
  • I'm glad that helps. It's always easier to show an example in it's entirety than to try and describe it. Good luck! – johnnelm9r Apr 30 '14 at 19:15
0

To go to rootViewController

[self.navigationController popToRootViewControllerAnimated:YES];
Vineesh TP
  • 7,755
  • 12
  • 66
  • 130