以编程方式创建uitableview

时间:2011-08-23 02:58:49

标签: iphone ios uiview uinavigationcontroller

您好,我正在测试如何开发一个应用程序,该应用程序具有多个具有自己的父/子结构的表视图,可以从主菜单访问。

我想知道如何使用uinavigationcontroller菜单生成新的tableview但是共享uitabbar,因为这是我第一次尝试这样的东西,通常我只是坚持使用apple模板。

以下是我想要实现的一般结构,任何评论建议代码示例都会非常感激,并且从那里我可以自己完成它,它更多的是关于我在哪里开始的问题:)

enter image description here

到目前为止,我已经设置了主窗口,其中UIAction捕获了不同的按钮点击,我需要弄清楚如何让所有孩子共享特定的UITabbar然后如何设置以便各个分支拥有自己的UINavigationController菜单,如果需要。

这是我的UIAction

//Delegate.m
//--- Caputer button clicks ---
- (IBAction)buttonClick: (UIButton *) sender
{
    if ([sender isEqual:orangeButton]) {
        NSLog(@"orangeButton Pressed");
    }
    else if ([sender isEqual:blueButton]) {
        NSLog(@"blueButton Pressed");
    }
    else if ([sender isEqual:greenButton]) {
        NSLog(@"greenButton Pressed");
    }
    else if ([sender isEqual:purpuleButton]) {
        NSLog(@"purpleButton Pressed");
    }
}

3 个答案:

答案 0 :(得分:1)

如果您创建了“基于TabBar的应用程序”,则为所需选项卡添加navigationController非常简单。在主窗口中拖动“UINavigationController”里面的“标签栏控制器”

enter image description here

关于“如何生成tableViews”,最简单的方法是创建一个通用的TableView,将其命名为 LevelTableView.h / .m 文件(它可能有自己的.xib),你可以在哪里添加你想要的东西,然后继续创建新的ViewControllers,例如,在这个tableView的级别中:

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    LevelTableView *anotherLevel = [[LevelTableView alloc] initWith...];
    [self.navigationController pushViewController:anotherLevel animated:YES];
    [anotherLevel release];
}

关键是,这个“LevelTableView”只创建了一次,但是为你想要添加的每个级别实例化了不同的时间,内容是唯一可以改变的东西。

答案 1 :(得分:0)

将导航控制器添加到标签栏控制器中。然后隐藏NavigationController并根据您的架构使用工具栏弹出并导航您的视图。

答案 2 :(得分:0)

我有类似的东西。我所做的是我创建了一个不同的类,我称之为“TabBarController”,并初始化了我的标签中的所有不同视图:

    UIViewController *viewController1 = [[FirstTab alloc] initWithNibName:@"FirstTab" bundle:NSBundle.mainBundle];
    UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:viewController1];        
    UIViewController *viewController2 = [[SecondTab alloc] initWithNibName:@"SecondTab" bundle:NSBundle.mainBundle];
    UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:viewController2];
    myTabBarController = [[UITabBarController alloc] init];
    myTabBarController.viewControllers = [NSArray arrayWithObjects:firstNavController, secondNavController, nil];    

然后,当用户点击主视图中的一个按钮时,我就是这样做的:

- (IBAction)yourAction:(id)sender {  

TabBarController *tabBarController1 = [[TabBarController alloc] init];
tabController.selectedIndex = 1; ==> chose which tab you want to show 
[[[UIApplication sharedApplication]delegate].window setRootViewController:tabBarController1.myTabBarController];

}

这项工作相当不错。