私はiPhone開発に不慣れで、特定の種類のアプリをまとめるための一般的なデザインパターン/ガイドについてアドバイスを得たいと思っていました.
TabBar タイプのアプリケーションを構築しようとしています。タブの 1 つは TableView を表示する必要があり、テーブル ビュー内からセルを選択すると、別の処理が行われます。おそらく、別のテーブル ビューまたは Web ページが表示されます。テーブル ビュー/Web ページから戻るには、ナビゲーション バーが必要です。
これまでに取ったアプローチは次のとおりです。
rootcontroller として UITabBarController に基づいてアプリを作成します。
すなわち
@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 を使用する必要がありますか? IB の MyControllerView.xib に NavigationBar をドロップしようとしましたが、アプリを実行しても表示されず、TableView のみが表示されます。