【问题标题】:How to Load View Controllers At the Beginning of the App?如何在应用程序的开头加载视图控制器?
【发布时间】:2013-03-04 07:50:03
【问题描述】:

我有一个选项卡视图控制器作为我在应用程序中的根控制器,它由 3 个选项卡组成,我们称它们为视图 A、视图 B、视图 C。 我想在我的应用程序启动后立即加载这些选项卡,我认为可以在 didFinishLaunchingWithOptions 函数中完成,但我不确定,有人知道怎么做吗?

谢谢

【问题讨论】:

    标签: xcode uiviewcontroller uitabbarcontroller uistoryboard


    【解决方案1】:

    出于这些目的,我使用了这种方法:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window = window;
    
        UITabBarController *tabBarController = [[UITabBarController alloc] init];
    
        rootView = [[SGMenuViewController alloc] init];
    
        UIViewController *tab1VC = [[UIViewController alloc] init];
        UIViewController *tab2VC = [[UIViewController alloc] init];
        UIViewController *tab3VC = [[UIViewController alloc] init];
        UIViewController *tab4VC = [[UIViewController alloc] init];
    
        navController = [[UINavigationController alloc] initWithRootViewController:rootView];
        UINavigationController *secondNavController = [[UINavigationController alloc] initWithRootViewController:tab3VC];
    
        NSArray* controllers = [NSArray arrayWithObjects:navController, tab2VC, secondNavController, tab4VC, nil];
        tabBarController.viewControllers = controllers;
    
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.rootViewController = tabBarController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    您基本上创建了一个UITabBarController,然后将所有视图放在那里。如果您需要在所选选项卡中推送/弹出视图,您可以创建 UINavigationController。在上面的示例中,只有1st3rd 选项卡有UINavigationController,所以我可以在它们上使用self.navigationController pushViewController:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多