示例代码和步骤
步骤 1 − 创建一个新项目并选择 Tabbed Application 而不是基于视图的应用程序,然后单击 next, 给出项目名称并选择 create.
步骤 2 - 这里默认创建了两个视图控制器,并在我们的应用程序中添加了一个标签栏。
步骤 3 − 该 AppDelegate.m didFinishLaunchingWithOptions 方法如下 -
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]
bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FirstViewController alloc]
initWithNibName:@"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc]
initWithNibName:@"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[viewController1,
viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
步骤 4 - 这里,两个视图控制器被分配并作为我们的标签栏控制器的视图控制器。
步骤 5 − 当我们运行应用程序时,我们将得到以下输出 −