ViewController間のデータのやりとり → | demicadeのブログ

demicadeのブログ

iPhoneアプリ開発素人のメモ的なブログです。

予定通り、ViewController間のデータのやりとりです。

ViewControllerから、SecondViewControllerにデータを渡します。



*********************************************

ViewController.h


*********************************************

ViewController.m

-(void)pushBtn1 {

  // SecondViewControllerを生成
SecondViewController *vc = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

// SecondViewControllerのプロパティtitleStringに文字列を設定
vc.titleString = @"Push Button1";

// ナビゲーションによる画面遷移を実行
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}


*********************************************

SecondViewController.h


@property(nonatomic,retain) NSString * titleString;

*********************************************

SecondViewController.m


@synthesize titleString = _titleString;

(中略)

- (void)dealloc
{
// メモリの片付け
[_titleString release];
[super dealloc];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;

// メモリの片付け
self.titleString = nil;
}

(中略)

-(void)setupParts {

// titleLblのテキストにViewControllerで設定したtitleStringを代入
titleLbl.text = self.titleString;

[self.view addSubview:titleLbl];
[titleLbl release];

(後略)
}


*********************************************

これで、SecondViewControllerに "Push Button1" と表示されます。

とある講座で習ったので、おすそわけです。