全然できなくてハマってしまった・・・・。
やりたいことはpopovercontrollerで作ったuitableviewの選択されたセルの値を、親画面に渡すというもの。セルが選択されたら親画面のインスタンスを参照して、そのプロパティをセットすれば画面の値もかわるもんだと思ってたら、そうじゃないのかな・・・。
以下ソース。該当する所だけ抜き出した。Window-based Applicationを使用しています。
HomeViewControllerが親画面で、popoverControllerで表示されるviewがMenuTable。
//HomeViewController.h @interface HomeViewController : UIViewController { UIPopoverController *popController; UIBarButtonItem *menuBtn; MenuTable *menuTable; UILabel *label; } @property(nonatomic,retain)UILabel *label; @end
//MenuTable.h @class HomeViewController; @interface MenuTable : UITableViewController { NSArray *keys; NSDictionary *dataSource; HomeViewController *homeViewController; } @property(nonatomic,retain)HomeViewController *homeViewController; @end //MenuTable.m //tableは2つセクションがあります。 @implementaion MenuTable //親画面のインスタンスをプロパティとして保持 @synthesize homeViewController; //セルが選択された時のイベントハンドラ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Navigation logic may go here. Create and push another view controller. id key = [keys objectAtIndex:indexPath.section]; NSString *labeltext =[[dataSource objectForKey:key] objectAtIndex:indexPath.row]; //値がかわんねー。全くかわらねー homeViewController.label.text = labeltext; }
このコードがだめな理由がお分かりの方、もしよろしければアドバイスください><;
追記
スピード解決しました!!@katax++。@kataxにならビールおごってもいい。
まず、MenuTableでhomeviewControllerのインスタンスを参照していなかったことが問題だった。むしろNULLだった。NULLだったらぬるぽおこしてくれたらいいのに。アノテーション脳で自動的にぶち込んでくれるんじゃねーのって思いこんだのが間違い。大きな間違い。
で、最初はこう書いた。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Navigation logic may go here. Create and push another view controller. //ここ追記。 homeViewController = [[[HomeViewController alloc]init]autorelease]; id key = [keys objectAtIndex:indexPath.section]; NSString *labeltext =[[dataSource objectForKey:key] objectAtIndex:indexPath.row]; //値がかわんねー。全くかわらねー homeViewController.label.text = labeltext; }
これだとセルが選択される度にnew()しているのと一緒だから、毎回違うメモリのアドレスが割り振られてしまい、AppDelegate内でalloc initしたアドレスと違うため、homeviewcontrollerのlabelが恐らくnilになっていたようだ。
このMenuTableがalloc initされた時に、AppDelegateでalloc initした時のアドレスを参照渡ししなくてはならない。そうしないとだめみたいだ。ここまでくれば、後は簡単。MenuTableをHomeViewControllerでalloc initした後に、自分自身のアドレスをMenuTable.homeViewControllerに渡せばいい。
menuTable = [[[MenuTable alloc]initWithStyle:UITableViewStylePlain] autorelease];
menuTable.homeViewController = self;
popController = [[UIPopoverController alloc] initWithContentViewController:menuTable];
できたーーーーーー!!!!