Question

I have a welcome screen that only shows the first time the user opens the app. The screen is working great, but I can't get it to show the normal screen when the user clicks done.

Here is the code in the app delegate to create the normal screen -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;

    if([[NSUserDefaults standardUserDefaults] boolForKey:@"TermsAccepted"]!=YES)
    {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"TermsAccepted"];
    }

    // Override point for customization after application launch.
    FeedViewController *feedViewController = [[FeedViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc]  initWithRootViewController:feedViewController];
    [self.window addSubview:nav.view];
    self.window.rootViewController = nav;

    [self.window makeKeyAndVisible];

    self.tabBarController = [[UITabBarController alloc] init];
    [[UITabBar appearance] setTintColor:[UIColor redColor]];


    // FeedViewController
    feedViewController=[[FeedViewController alloc] init];
    feedViewController.tabBarItem.image=[UIImage imageNamed:@"Describe-Home_Icon_NormalArtboard-1"];
    feedViewController.title = @"Timeline";
    feedViewController.tabBarItem.title = nil;

    //TodayViewController
    TodayViewController *todayViewController = [[TodayViewController alloc] init];
    todayViewController.tabBarItem.image = [UIImage imageNamed:@"Today_Icon"];
    todayViewController.title = @"Today";
    todayViewController.tabBarItem.title = nil;

    //CreateViewController
    self.createViewController = [[CreateViewController alloc] init];
    self.createViewController.tabBarItem.image = [UIImage imageNamed:@"Create_Icon"];
    self.createViewController.title = @"Create";
    self.createViewController.tabBarItem.title = nil;

    //AlertViewController
    AlertsViewController *alertsViewController = [[AlertsViewController alloc] init];
    alertsViewController.tabBarItem.image=[UIImage imageNamed:@"Alerts_IconArtboard-1"];
    alertsViewController.title=@"Alerts";
    alertsViewController.tabBarItem.title = nil;

    //ProfileViewController
    ProfileViewController *profileViewController = [[ProfileViewController alloc] init];
    profileViewController.tabBarItem.image=[UIImage imageNamed:@"Profile_IconArtboard-1"];
    profileViewController.title=@"Profile";
    profileViewController.tabBarItem.title = nil;

    NSMutableArray *tabBarViewControllers = [[NSMutableArray alloc] initWithCapacity:2];

    self.tabBarController = [[UITabBarController alloc] init];


    UINavigationController *feedNavigationController = [[UINavigationController alloc] initWithRootViewController:feedViewController];
    [tabBarViewControllers addObject:feedNavigationController];
    feedNavigationController = nil;

    UINavigationController *todayNavigationController = [[UINavigationController alloc] initWithRootViewController:todayViewController];
    [tabBarViewControllers addObject:todayNavigationController];
    todayNavigationController = nil;

    UINavigationController *createNavigationController = [[UINavigationController alloc] initWithRootViewController:self.createViewController];
    [tabBarViewControllers addObject:createNavigationController];
    createNavigationController = nil;

    UINavigationController *alertsNavigationController = [[UINavigationController alloc] initWithRootViewController:alertsViewController];
    [tabBarViewControllers addObject:alertsNavigationController];
    alertsNavigationController = nil;

    UINavigationController *profileNavigationController = [[UINavigationController alloc] initWithRootViewController:profileViewController];
    [tabBarViewControllers addObject:profileNavigationController];
    profileNavigationController = nil;

    self.tabBarController.viewControllers = tabBarViewControllers;
    tabBarViewControllers = nil;

    [self.window addSubview:self.tabBarController.view];

    return YES;
}

In feedViewController to push the welcome view controller -

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"TermsAccepted"]){
    NSLog(@"Second time opening the app");
}
else{
    WelcomeViewController *welcomeViewController = [[WelcomeViewController alloc] init];
    [self.navigationController pushViewController:welcomeViewController animated:NO];
}

To go back to feed that isn't working -

-(void)showDone:(UIButton *)sender {

    if (self.navigationItem.rightBarButtonItem.tintColor == [UIColor redColor]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"TermsAccepted"];

        FeedViewController *feedViewController = [[FeedViewController alloc] init];
        [[UIApplication sharedApplication] keyWindow].rootViewController = feedViewController;

        self.tabBarController.tabBar.hidden = NO;
        self.navigationController.navigationBar.hidden = NO;
    }
}
Was it helpful?

Solution

It looks like when you show your Welcome screen, you push it onto a UINavigatonController. But when you click Done, you're trying to set the root view controller on the window instead of just popping the view controller from the navigation controller. It also looks like you're creating a new instance of your FeedViewController instead of using the instance you already have created.

Also, have you checked to see if it's even executing the code in your showDone: method? You're using == to compare the tintColor of a UIBarButtonItem with a UIColor, but using == will only return true if they're both the exact same UIColor instance, which they might not be. You'll want to use the method isEqual: to compare two colors, so you would do the following instead:

[self.navigationItem.rightBarButtonItem.tintColor isEqual:[UIColor redColor]]

Note that this won't always return YES for equal colors if they're in different color spaces, but most of the time this should work.

Also, you should consider moving your code out of your app delegate, because generally application:didFinishLaunchingWithOptions: is used only for things that need to be done immediately upon startup. It shouldn't be used to initialize a bunch of view controllers, those should only be initialized when it's time for them to be displayed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top