2014-02-08 75 views
-1

我有一個歡迎屏幕,只顯示用戶第一次打開該應用程序。屏幕效果很好,但當用戶點擊完成後,我無法顯示正常屏幕。從歡迎屏幕推動視圖控制器時出現問題

這裏是應用程序委託代碼來創建正常畫面 -

- (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"]; 
    [email protected]"Alerts"; 
    alertsViewController.tabBarItem.title = nil; 

    //ProfileViewController 
    ProfileViewController *profileViewController = [[ProfileViewController alloc] init]; 
    profileViewController.tabBarItem.image=[UIImage imageNamed:@"Profile_IconArtboard-1"]; 
    [email protected]"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]; 
} 

要回去喂,是不是工作 -

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

' - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions'不是設計用於容納初始化應用程序所需的所有操作的槽。 – nhgrif

回答

0

它看起來像當你顯示你的歡迎屏幕,你把它推到UINavigatonController。但是,當您單擊完成時,您正嘗試在窗口上設置根視圖控制器,而不是從導航控制器中彈出視圖控制器。它也看起來像你正在創建你的FeedViewController的新實例,而不是使用你已經創建的實例。

此外,您是否檢查過它是否執行您的showDone:方法中的代碼?您正在使用==來比較UIBarButtonItemtintColorUIColor,但使用==只會返回true,前提是它們兩個完全相同,但它們可能不是UIColor。您將要使用的方法isEqual:來比較兩種顏色,所以你會怎麼做,而不是執行以下操作:

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

注意,這不會總是返回YES平等的顏色,如果他們在不同的色彩空間,但大部分時間這應該工作。

另外,您應該考慮將您的代碼移出應用程序委託,因爲通常application:didFinishLaunchingWithOptions:僅用於啓動時需要立即執行的操作。它不應該用來初始化一堆視圖控制器,只有在它們被顯示的時候才應該初始化它們。