我怎样才能在viewController中使用UITabbar?

时间:2012-12-11 21:20:19

标签: iphone ios uitableview uitabbar

如果我直截了当地说我的问题是我有一个带有两个按钮的UIViewController。

  1. 按钮一 - (onclick必须导航到)---> UITabbarController带有UITabbar项目,或者我可以在我的新{{1}中使用UITabbar }};

  2. 按钮2 - (OnClick必须导航到)---> UIViewController其中第一个UITabbarController将包含tabbaritem或者我可以带{{1}在我的新UITableView中,首先UITabbar将包含UIViewController

  3. 我该如何制作?示例代码将更有帮助既不建议。如果您需要任何澄清,请不要犹豫。

    最诚挚的问候 卫门

1 个答案:

答案 0 :(得分:1)

首先,如果您希望在单击按钮时按下另一个视图控制器,则需要具有NavigationController。您需要执行的步骤如下

  1. 从iPhone上的MasterDetail应用开始
  2. 在xib文件中,按下按钮并将它们连接到正确的方法
  3. 在视图中确实出现了视图控制器具有以下代码

    self.tbc1 = [[UITabBarController alloc] init];
    self.tbc2 = [[UITabBarController alloc] init];
    
    
    UIViewController *tbc1vc1 = [[UIViewController alloc] init];
    UIViewController *tbc1vc2 = [[UIViewController alloc] init];
    UIViewController *tbc1vc3 = [[UIViewController alloc] init];
    
    UITableViewController *tbc2vc1 = [[UITableViewController alloc] init];
    UIViewController *tbc2vc2 = [[UIViewController alloc] init];
    UIViewController *tbc2vc3 = [[UIViewController alloc] init];
    
    [self.tbc1 setViewControllers:[NSArray arrayWithObjects:tbc1vc1,tbc1vc2,tbc1vc3, nil]];
    [self.tbc1 setViewControllers:[NSArray arrayWithObjects:tbc2vc1,tbc2vc2,tbc2vc3, nil]];
    
  4. 当然,您必须在视图控制器中为tbc1和tncb2定义属性。

  5. 在app delegate中执行以下操作

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    ViewController *myViewController = [[ViewController alloc] init];
    self.viewController = [[UINavigationController alloc] initWithRootViewController:myViewController];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
    
  6. 将.h文件中的UIViewController更改为UINavigationController,如

    @property (strong, nonatomic) UINavigationController *viewController;
    
  7. 有两个按钮并将其连接到以下方法

    -(IBAction)tbc1Clicked{
    [self.navigationController pushViewController:self.tbc1 animated:YES];
    }
    
    -(IBAction)tbc2Clicked{
        [self.navigationController pushViewController:self.tbc2 animated:YES];
    }