ios - 创建一个仅在第一个应用程序打开时出现的 View Controller

标签 ios uiviewcontroller

我正在创建一个仅在用户第一次打开应用程序时出现的 View Controller 。我只是通过推送欢迎 View Controller 让它工作,但是当用户在 welcomeViewController 中完成时我无法让它推送 normalViewController。然后我尝试将 welcomeViewController 设置为 rootViewController。这仍然显示欢迎屏幕,但上面的所有按钮都不起作用。如果有人知道如何解决此问题或创建 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];

最佳答案

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

关于ios - 创建一个仅在第一个应用程序打开时出现的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21662921/

相关文章:

objective-c - 向 UIViewController 及其所有子类添加属性

ios - 更改 UIWindow 的 rootViewController

iphone - 如何修改现有的 UIViewController 以允许滚动?

ios - 在 iOS 7 中维护导航动画

ios - Swift:更改 TableView 标题文本颜色 - 崩溃

iphone - Google 帐户 OAuth2 支持框架?

ios - Swift firebase从数据库检索数据时,如何延迟下一个代码直到检索完成?

iOS 奇怪地读取一些字符?

ios - 如何在 Swift 3 中使用 SwiftyJSON 搜索 JSON 元素

Swift:当前 View Controller 在转换到下一个 View Controller 期间重新显示