navigation stackへの追加
前回UINavigationController, TableViewControllerの作成-CS193Pの続きになります。ViewControllerインスタンスをnavigation stackへと追加するところが中心となります。
iPhone Application Programming
今迄、作成したソースはhttp://public.me.com/seijit/iPhone/CS193Pから
今回の目標
下の図のようなアプリケーションの内、PersonListを選択し、DetailViewを表示できるようになるのが目標です。(右側の図)

ViewControler,xibの作成
今迄と同じようなことなので、図は省略します。
- TemplateからViewControllerのSubClassを選択し、名前を"PersonDetailViewController"とします。
- TemplateからView XIBを選択し、名前を"PersonDetail.xib"とします。
PersonDetailViewControllerの実装
新しく作成したPersonDetailViewControllerを実装します。
PersonDetailViewController.h
#import <UIKit/UIKit.h>
@interface PersonDetailViewController : UIViewController {
NSDictionary* dict;
IBOutlet UILabel *name;
IBOutlet UIImageView *imageView;
}
@property (nonatomic, assign) NSDictionary* dict;
@property (nonatomic, retain) UILabel *name;
@property (nonatomic, retain) UIImageView *imageView;
@end
PersonDetailViewController.m
viewがLoadされた後にNSDictionaryのデータをOutletに展開しています。
#import "PersonDetailViewController.h"
@implementation PersonDetailViewController
@synthesize dict;
@synthesize name;
@synthesize imageView;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationItem setTitle:@"Detail"];
[name setText:[self.dict objectForKey:@"name"]];
[imageView setImage:[UIImage imageWithContentsOfFile:[self.dict objectForKey:@"path"]]];
}
// 間省略
- (void)dealloc {
[name release];
[imageView release];
[super dealloc];
}
@end
PersonDetailViewControllerとViewとの接続
PersonDetail.xib
PersonDetail.xibをWクリックしInterfaceBuilderを起動します。Viewを例えば下の図のようにします。

その後、下の作業を行います。
- File's Ownerのクラス名をPersonDetailViewControllerとする。
- PersonDetailViewControllerの"view"をUIViewに接続する。
- PersonDetailViewControllerの"name", "imageView"をそれぞれUILabel, UIImageViewに接続する。
PersonListViewControllerの修正
今回作成した、PersonDetailViewControllerはこのままではLoadされません。このPresenceアプリケーションに組み込む必要があります。
PersonListViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
PersonDetailViewController *detailViewController = [[PersonDetailViewController alloc] initWithNibName:@"PersonDetail" bundle:nil];
int personIndex = [indexPath indexAtPosition: [indexPath length] - 1];
detailViewController.dict = [myData objectAtIndex:personIndex];
[self.navigationController pushViewController:detailViewController animated:NO];
[detailViewController release];
}
これはTableViewのCellを選択した場合にCallされるメソッドです。PersonDetailViewController.hをImportすることも必要です。
これで下のように動作するかと思います。

今回作成したソース
http://public.me.com/seijit/iPhone/CS193P/Lecture7:Presence2.tar.gz
TrackBack URL :

No comments yet.
Leave a comment
This is the comment form.