创建仅在第一个应用程序上打开的视图控制器

时间:2014-02-09 18:14:06

标签: ios uiviewcontroller

我正在创建一个仅在用户首次打开应用时出现的视图控制器。我只是按下欢迎视图控制器就可以了,但是当用户在welcomeViewController中完成时,我无法推送它来推送normalViewController。然后我尝试将welcomeViewController设置为rootViewController。这仍然显示welcomeScreen,但它上面的所有按钮都不起作用。如果任何人知道如何解决这个问题或者更好的方法来创建welcomeViewController,那将非常感激。这是我用来显示rootViewController

的代码
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"TermsAccepted"]){
        NSLog(@"Second time opening the app");
    }
    else{
        WelcomeViewController *welcomeViewController = [[WelcomeViewController alloc] init];
        [[UIApplication sharedApplication] keyWindow].rootViewController = welcomeViewController;
    }

创建UITabBar -

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];

1 个答案:

答案 0 :(得分:1)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

   if ([[NSUserDefaults standardUserDefaults] boolForKey:@"TermsAccepted"]){
      NSLog(@"Second time opening the app");
      rootViewController = // Your new main controller
   }
   else
   {
      rootViewController = // Terms and conditions view controller
   }

    self.window.rootViewController = rootViewController;

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    self.window.rootViewController = navigationController;
    return YES;
}