IOS:在secondviewcontroller中启动tabbarcontroller

时间:2013-07-02 10:04:14

标签: ios uitabbarcontroller uitabbar

我想在我的应用中设置一个标签栏,在我的appDelegate中我这样做:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
    UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];
    [viewController2 viewDidLoad];
    UIViewController *viewController3 = [[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil] autorelease];
    [viewController3 viewDidLoad];
    UIViewController *viewController4 = [[[FourthViewController alloc] initWithNibName:@"FourthViewController" bundle:nil] autorelease];
    [viewController4 viewDidLoad];
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, viewController4];

    self.viewController = [[[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil] autorelease];

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

你可以看到我想立即使用标签栏启动我的应用程序,事实上我用HomeViewController启动我的应用程序

在我的HomeViewController中我打开viewController1:

FirstViewController *first = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
    [self presentViewController:first animated:YES completion:nil];
    [first release];

但在我的firstviewcontroller中,我没有看到我的tabbar,为什么? (我声明如果我没有homeviewcontroller,应用程序可以正常使用tabbar)

1 个答案:

答案 0 :(得分:1)

你没有看到tabbar,因为在HomeViewController中你创建了FirstViewController的新实例并以模态方式呈现它。您应该在HomeViewController中创建并呈现整个UITabBarController:

的AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    self.viewController = [[[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil] autorelease];

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}
HomeViewController中的

FirstViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
SecondViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];
ThirdViewController *viewController3 = [[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil] autorelease];
FourthViewController *viewController4 = [[[FourthViewController alloc] initWithNibName:@"FourthViewController" bundle:nil] autorelease];

self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, viewController4];

[self presentViewController:self.tabBarController animated:YES completion:nil];