1

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

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

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

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

IE

@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 拖放到 IB 中的 MyControllerView.xib 上,但在运行应用程序时未显示,仅显示 TableView。

4

1 回答 1

0

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

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

于 2010-08-27T10:46:33.570 回答