如何从第二个视图控制器添加标签栏控制器

时间:2013-11-12 10:12:49

标签: ios objective-c cocoa-touch ios6

我是IOS app开发学习的初学者。 我有一个登录屏幕作为我的第一个视图控制器,我需要第二个视图控制器作为标签栏视图控制器。有4个不同的标签,我有4个不同的XIB。

有人帮助我继续前进。

4 个答案:

答案 0 :(得分:2)

您可以做的最佳方式是在应用程序从标签栏控制器第一个屏幕启动时以模态方式显示登录屏幕,在viewWillAppear中添加用于显示登录屏幕的代码,并在登录后关闭屏幕。你可以在appDelegate中像这样创建TabBarController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{


    UITabBarController tabBarController=[[UITabBarController alloc] init]; 


    FirstViewController *firstVC =  [[UIViewController alloc] initWithNibName:@"FirstVC" bundle:nil];
    UINavigationController *firstNavController = [[UINavigationController alloc] initWithRootViewController: firstVC];     

    SecondViewController *secondVC = [[UIViewController alloc] initWithNibName:@"secondVC" bundle:nil];
    UINavigationController *secondNavController = [[UINavigationController alloc] initWithRootViewController:secondVC]; 

   tabBarController.viewControllers = [NSArray arrayWithObjects: firstNavController, secondNavController, nil];

    tabBarController.selectedIndex=0;
    tabBarController.delegate = self;

UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"Movies" image:[UIImage imageNamed:@"MoviesTAB.png"] tag:1];

    [firstVC  setTabBarItem:item1];

    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"Music" image:[UIImage imageNamed:@"musicTAB.png"] tag:2];
    [seconfVC setTabBarItem:item2];

    tabController.tabBar.translucent  = NO;
    tabController.tabBar.barStyle = UIBarStyleBlack;
    tabBarController.tintColor = [UIColor whiteColor];

    self.window.rootViewController = tabController;
return YES;
}

答案 1 :(得分:0)

最好的方法是使用故事板,只有一个初始的UIViewController,并从中使segue到UITabBarViewController。 http://youtu.be/a_DCTSTv1Mw

答案 2 :(得分:0)

如果你想通过xib创建一个UITabBarViewController并将viewControllers添加到该UITabBarViewController对象的对象数组中。

示例代码:

    NSMutableArray *arrViewControllers = [[NSMutableArray alloc] init];

    UIViewController *tabController;
    UIImage *tabImage ;
    NSString *tabTitle;

    for (int i= 0; i < 3; i++) {
        switch (i) {
            case 0:
                tabController = [[ViewController alloc] init];
                tabImage = [UIImage imageNamed:@"icon1.png"];
                tabTitle = @"Text";
                break;

            case 1:
                tabController = [[ImageDemoViewController alloc] init];
                tabImage = [UIImage imageNamed:@"icon2.png"];
                tabTitle = @"Image";
                break;
            case 2:
                tabController = [[TableDemoViewController alloc] init];
                tabImage = [UIImage imageNamed:@"icon3.png"];
                tabTitle = @"Table";
                break;
        }
        // set the image and title using some properties of UIViewController

        tabController.tabBarItem.image = tabImage;
        tabController.tabBarItem.title = tabTitle;

        //add objects to array

        [arrViewControllers addObject:tabController];

        [tabController release];
    }

    _baseController = [[UITabBarController alloc] init];
    _baseController.viewControllers =  arrViewControllers;

    [arrViewControllers release];

答案 3 :(得分:0)

转到你的appDelegate

1.为登录界面创建一个viewController。

LoginViewController *viewController1 = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];

2.使用root视图创建一个navigationController,登录ViewController。

UINavigationController *nVC = [[UINavigationController alloc] initWithRootViewController:viewController1];

3.将navigationController设置为窗口的根视图。

self.window.rootViewController = self.nVC;
    [self.window makeKeyAndVisible];

现在转到LoginViewController中的登录按钮的Touch-Up-Inside方法。

1.验证密码后,userId初始化viewControllers以获取tabbar和TabbarViewController。

UiViewController ...*yourViewControllers,..,
UITabBarController *YourtabBarController = [[UITabBarController alloc] init];

2.现在将这些viewControllers添加到tabbarController。

 YourtabBarController.viewControllers = @[  YourViewController1,YourViewController2,YourViewController3,......];

3.最后将此tabbarController推送到navigationControllere。

[self.navigationController pushViewController:YourtabBarController animated:NO];