将NavigationControl添加到包含UITableViews的TabBar应用程序

时间:2009-08-13 08:46:01

标签: uitableview uiviewcontroller uinavigationbar

我是iPhone开发人员的新手,希望获得有关将某种应用程序放在一起的一般设计模式/指南的建议。

我正在尝试构建一个TabBar类型的应用程序。其中一个选项卡需要显示一个TableView,并从表视图中选择一个单元格将执行其他操作 - 可能显示另一个表视图或网页。我需要一个导航栏才能从表格视图/网页上取回我。

到目前为止,我采取的方法是:

创建一个基于UITabBarController的应用程序作为root控制器

@interface MyAppDelegate : NSObject <UIApplicationDelegate> 
{
IBOutlet UIWindow *window;
IBOutlet UITabBarController *rootController;
}

创建一个UIViewController派生类和相关NIB的加载,并在IB中连接所有内容,这样当我运行应用程序时,我可以使基本选项卡正常工作。

然后我接受UIViewController派生类并将其修改为以下内容:

@interface MyViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> 
{

}

我将委托方法添加到MyViewController的实现

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section         { 
return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

if (indexPath.row == 0)
{
    cell.textLabel.text = @"Mummy";     
}
else
{
    cell.textLabel.text = @"Daddy";
}
return cell;
}

返回IB,打开MyViewController.xib并将UITableView放到其上。将Files Owner设置为MyViewController,然后将UITableView的委托和数据源设置为MyViewController。

如果我现在运行应用程序,我会看到桌面视图与木乃伊和爸爸一起工作得很好。到目前为止一切都很好。

问题是我如何在实施时将导航栏合并到我当前的代码中:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath()
{
//  get row selected
NSUInteger row = [indexPath row];

if (row == 0)
{
    //  Show another table
}
else if (row == 1)
{
    //  Show a web view
}
}

我是否将NavigationBar UI控件放到MyControllerView.xib上?我应该以编程方式创建它吗?我应该在某处使用UINavigationController吗?我已经尝试将一个NavigationBar放到我的MyControllerView.xib中,但是当我运行应用程序时它没有显示,只显示了TableView。

1 个答案:

答案 0 :(得分:0)

http://miketeo.net/wp/index.php/2008/08/31/simple-iphone-tutorial-part-3.html

这段代码对我很有帮助。您可以下载并使用此代码..

相关问题