无法从欢迎屏幕推送视图控制器

时间:2014-02-08 19:32:56

标签: ios uiviewcontroller appdelegate

我有一个欢迎屏幕,仅在用户首次打开应用时显示。屏幕工作得很好,但是当用户点击完成时我无法显示正常的屏幕。

以下是app delegate中用于创建普通屏幕的代码 -

- (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;
}

在feedViewController中推送欢迎视图控制器 -

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

返回无法使用的Feed -

-(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;
    }
}

1 个答案:

答案 0 :(得分:0)

看起来当您显示欢迎屏幕时,将其推送到UINavigatonController。但是当您单击“完成”时,您尝试在窗口上设置根视图控制器,而不是仅从导航控制器弹出视图控制器。看起来您正在创建FeedViewController的新实例,而不是使用您已创建的实例。

另外,您是否检查过它是否甚至在showDone:方法中执行代码?您使用==tintColor的{​​{1}}与UIBarButtonItem进行比较,但使用UIColor只有在确切时才会返回true相同的==实例,它们可能不是。您将需要使用方法UIColor来比较两种颜色,因此您将执行以下操作:

isEqual:

请注意,如果它们位于不同的颜色空间中,则不会始终返回[self.navigationItem.rightBarButtonItem.tintColor isEqual:[UIColor redColor]] 相同的颜色,但大部分时间都应该有效。

此外,您应该考虑将代码移出应用代理,因为通常YES仅用于需要在启动时立即执行的操作。它不应该用于初始化一堆视图控制器,只有在它们显示它们时才应该初始化它们。