• 締切済み

iPhoneアプリ 画面遷移での変数受け渡し

現在、iPhoneアプリの開発を勉強している者です。 少し詰まってしまった箇所があるので、ご教授いただけると幸いです。 TableViewController間での変数の受け渡しを実装しており、1枚目のTableViewでタップされたセルのテキストを2枚目に渡す、というコードを書きました。 受け渡しに関するコードは以下の部分です。 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self performSegueWithIdentifier:@"mySegue" sender:self]; } -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"mySegue"]) { SecondTableViewController *viewCon = segue.destinationViewController; NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; viewCon.myValue = cell.textLabel.text; } } これ自体は問題なく動いてくれたのですが、SecondTableViewControllerにNavigationControllerをEmbed Inしたところ、1枚目でセルをタップした際にエラーが出てアプリが止まってしまうようになりました。 BreakPointを設けてみると上記コードのviewCon.myValue = cell.textLabel.text;のところで止まっており、unrecognized selector sent to instanceというエラーが吐き出されていました。 同様のエラーが出た例などを調べてみたのですが自力では解決できず・・・、どなたか原因を指摘して頂けるとありがたいです。 よろしくお願いします。

みんなの回答

回答No.1

NavigationControllerをEmbed Inしたんだったら、 segue.destinationViewControllerで得られるビューコントローラーは、 NavigationControllerです。 SecondTableViewControllerは、そのNavigationController配下の topViewControllerに配置されます。 つまり、 SecondTableViewController *viewCon = segue.destinationViewController; の部分を UINavigationController *naviCon = segue.destinationViewController; SecondTableViewController *viewCon = (SecondTableViewController *)naviCon.topViewController; のようにする必要があります。

katsuo777
質問者

お礼

詳しくありがとうございます。 なるほど・・・その通りですね。不勉強で、NavigationControllerの構造をきちんと理解できていませんでした。 教えていた通りに実装したらうまく動いてくれました。 ありがとうございました!

関連するQ&A

  • iphoneアプリ開発での変数の受け渡し

    最近iPhoneアプリ開発を始めた者です。 変数の受け渡しについて質問があり、投稿させていただきました。 以下のコード内で、 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 以下で設定したIDに入ったデータを、 -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)senderの中のIDに受け渡したいのですが、どうやら2つのIDが繋がっていません。 自分自身あまりまだ理解できていないこともあり、ざっくりとした聞き方で申し訳ありません。 初歩的な質問だとは思うのですが、なにとぞご教授お願いします。 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (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]; } if (indexPath.row == 0){ cell.textLabel.text = @"東京"; }else{ cell.textLabel.text = @"大阪"; } int ID = indexPath.row; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self performSegueWithIdentifier:@"mySegue" sender:self]; } -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"mySegue"]) { SecondTableViewController *viewCon = segue.destinationViewController; viewCon.myValue = ID; } }

  • iphoneアプリ開発についての質問です

    tableviewcellにチェックマークを付けるだけの簡単なコードですが、なぜかチェックマークを付けたセルとは別のセルにまでチェックマークがついてしまっています。 また、激しくスクロールするとさっきまで付いていたチェックマークが外れていたりとさらに酷い状態に・・・。 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.accessoryType == UITableViewCellAccessoryCheckmark) cell.accessoryType = UITableViewCellAccessoryNone; else cell.accessoryType = UITableViewCellAccessoryCheckmark; [tableView deselectRowAtIndexPath:indexPath animated:YES]; } いろんなサイトで見ても上記のコードが最もよく見るコードでした。 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = @"ButtonCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; [self updateCell:cell :CellIdentifier atIndexPath:indexPath]; return cell; } - (void)updateCell:(UITableViewCell *)cell :(NSString *)CellIdentifier atIndexPath:(NSIndexPath *)indexPath { BButton *type = (BButton *)[cell viewWithTag:1]; //Buttonはライブラリを使用 [ReturnButton typeButton:type :self.showArr[indexPath.row]]; } チェックマーク以外はUIButtonなども正常に表示されました。 心当たりがあればお願いします。

  • objective-Cについてご相談があります。

    xcode6でMasterDetailApplicationを試しております。 Master画面のセルをクリックしてDetail画面に遷移した際、 標準でDetail画面の中央に貼られているLabelに、クリックした セル内の文字列が表示される様にしたいのです。 しかし、一度目のクリックでは表示されず、二度目のクリックで初めて 一度目にクリックしたセルの文字列がLabelに表示されてしまいます。 つまり見た目上は、Labelの表示がクリックより一回分遅れるのです。 ※添付画像を参考にして下さい 以下がその部分のコードです。 ----------------------------------------------------------------------- // セグエの設定 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { [self tableView]; // セグエを「showDetail」と名付ける if ([[segue identifier] isEqualToString:@"showDetail"]) { // _cellMojiはid型のインスタンス変数。 [[segue destinationViewController] setDetailItem:_cellMoji]; NSLog(@"1番:%@",_cellMoji); } } // セル内の文字列を取得するメソッド - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // セル内の文字列を取得する UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; // 文字列データをNSString型に変換する _cellMoji = cell.textLabel.text; NSLog(@"2番:%@",_cellMoji); } ---------------------------------------------------------------------- 以上、どなたか解決策をご教示頂けないでしょうか。 何卒宜しくお願い致します。

  • Objective-CのTableViewについて

    つまづいてなかなか先に進めません。回答の方をよろしくお願いします。 [ソース] - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { switch ([indexPath section]) { case 1: { switch ([indexPath row]) { case 0: { DropDownCell *cell = (DropDownCell*) [tableView cellForRowAtIndexPath:indexPath]; NSIndexPath *path0 = [NSIndexPath indexPathForRow:[indexPath row]+1 inSection:[indexPath section]]; NSIndexPath *path1 = [NSIndexPath indexPathForRow:[indexPath row]+2 inSection:[indexPath section]]; NSIndexPath *path2 = [NSIndexPath indexPathForRow:[indexPath row]+3 inSection:[indexPath section]]; NSArray *indexPathArray = [NSArray arrayWithObjects:path0, path1, path2, nil]; if ([cell isOpen]) { [cell setClosed]; dropDown1Open = [cell isOpen]; [tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop]; } else { [cell setOpen]; dropDown1Open = [cell isOpen]; [tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop]; } break; } default: { dropDown1 = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text]; NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:[indexPath section]]; DropDownCell *cell = (DropDownCell*) [tableView cellForRowAtIndexPath:path]; [[cell textLabel] setText:dropDown1]; NSIndexPath *path0 = [NSIndexPath indexPathForRow:[path row]+1 inSection:[indexPath section]]; NSIndexPath *path1 = [NSIndexPath indexPathForRow:[path row]+2 inSection:[indexPath section]]; NSIndexPath *path2 = [NSIndexPath indexPathForRow:[path row]+3 inSection:[indexPath section]]; NSArray *indexPathArray = [NSArray arrayWithObjects:path0, path1, path2, nil]; [cell setClosed]; dropDown1Open = [cell isOpen]; [tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop]; break; } } } } [tableView deselectRowAtIndexPath:indexPath animated:YES]; } このようなプログラムを書いたのですが、 UITableViewのCellがなぜか反転してしまいます。 つまり、画像のような状態になった後、再びCellを押すと閉じ、さらに押すともう一度画像のような状態になるのですが、Object1 → Object3 という状態から Object3 → Object1 と順番が逆になってしまうのです。 全く理由がわかりません。ぜひとも回答の方をお願い致します。 初めての投稿ですので、どのように説明していけばいいのかわかりませんでした。 ですが、どうしても直したい問題ですので、よろしくお願い致します。

  • iphoneアプリ開発についての質問です

    storyboardのprototype cellsでカスタムセルを4つ作成しidentifierも設定した状態で、 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static const id identifiers = { @"MetaCell", @"StatusCell", @"EvoCell", @"FormCell" }; NSString *CellIdentifier = identifiers[indexPath.section]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 以下略 という風にしているのですが UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: というエラーが出てしまいます。 identifierの文字列は何度見ても一致していました。 if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 上記のコードを追加しても、一応エラーは出ませんが当然セルは反映せず真っ白のままです。 if (cell == nil) { if (indexPath.section == 0) { cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; } else if (indexPath.section == 1) { cell = [tableView dequeueReusableCellWithIdentifier:@"StatusCell"]; } else if (indexPath.section == 2) { cell = [tableView dequeueReusableCellWithIdentifier:@"EvoCell"]; } else if (indexPath.section == 3) { cell = [tableView dequeueReusableCellWithIdentifier:@"FormCell"]; } } 一応このようなやりかたでも試しましたが結果は同じでした。 ちなみにこのテーブルビューへは前のビューからsegueで (void)[[segue destinationViewController] initDetailDic:dic]; で遷移してきています。 何度調べても原因がわからず困っています。心当たりはないでしょうか。

  • iphoneアプリ開発についての質問です

    http://okwave.jp/qa/q8846564.htmlの質問に補足を付け加えたものです。 storyboardのprototype cellsでカスタムセルを4つ作成しidentifierも設定した状態で、 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static const id identifiers = { @"MetaCell", @"StatusCell", @"EvoCell", @"FormCell" }; NSString *CellIdentifier = identifiers[indexPath.section]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 以下略 という風にしているのですが UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: というエラーが出てしまいます。 identifierの文字列は何度見ても一致していました。 if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 上記のコードを追加しても、一応エラーは出ませんが当然セルは反映せず真っ白のままです。 if (cell == nil) { if (indexPath.section == 0) { cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; } else if (indexPath.section == 1) { cell = [tableView dequeueReusableCellWithIdentifier:@"StatusCell"]; } else if (indexPath.section == 2) { cell = [tableView dequeueReusableCellWithIdentifier:@"EvoCell"]; } else if (indexPath.section == 3) { cell = [tableView dequeueReusableCellWithIdentifier:@"FormCell"]; } } 一応このようなやりかたでも試しましたが結果は同じでした。 ちなみにこのテーブルビューへは前のビューからsegueで (void)[[segue destinationViewController] initDetailDic:dic]; で遷移してきています。 何度調べても原因がわからず困っています。心当たりはないでしょうか。 *補足 問題の起こっているこのテーブルビューへは、前のテーブルビューから遷移してきているのですが試しにNavigationControllerのrootViewに設定してみたところちゃんと表示されました。画面遷移の方法に原因があると考えられるのですが、上に書いたやり方はごくごく一般の方法だとおもうのですが・・・

  • iphoneアプリ開発についての質問です

    XCodeでUISearchBarをUITableViewヘッダーにおいてテーブルビューの要素を検索したいのですがエラーになります。 サイズがテキスト量によって可変するカスタムセルをStoryboardで作れないのでコードで作成したんですが、その場合だとサーチバーにテキストを入力し検索を押した時点でエラーになり *** Assertion failure in -[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:], となってしまいます。 セル生成のコードは以下になります - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; //ココでエラー NSDictionary *dic; if (tableView == self.searchDisplayController.searchResultsTableView) { dic = self.searchResult[indexPath.row]; } else { dic = self.itemArr[indexPath.row]; } cell.title.text = dic[@"title"]; cell.label.text = dic[@"detail"]; CGSize textSize = [dic[@"detail"] sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(300, 200) lineBreakMode:NSLineBreakByWordWrapping]; cell.label.frame = CGRectMake(10, 35, textSize.width, textSize.height); return cell; } Storyboard上のprototypecellなら問題なく動作するのですが、どうしてもコード生成のカスタムセルだとエラーになってしまうようです。 アドバイスお願いします。

  • カスタムセル内のLabelの値の取り出し

    iOSアプリ開発において解決できない課題が出てきたので、なにかご助言をいただけるとありがたいです。 やりたいことは、TableViewにおいて、タップされたカスタムセル内のラベルの値を取り出す、ということです。 ネット上にこのやり方について説明したページはいくつもあり、その通りに実装してみたのですがうまくいきません。 コードの該当箇所は以下です。 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomCell"; // カスタムセルにキャスト。キャストすることによりカスタムセルのpropertyが使える CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; NSLog(@"%@",cell.label.text); } ビルドしてセルをタップすると、どのセルであってもlabelの初期値である"Label"としか出力されないです。 シミュレータでのセルの表示自体は問題ないです。 なにか原因として考えられることがあれば指摘していただけるとありがたいです。 どうかよろしくお願いします。

  • アコーディオン風のtableviewの遷移について

    現在、アコーディオン風の階層的なtableviewからタップで画面を遷移するコードを作っています。 一番最初にでてくる画面のセルにAとBがでてきて、AをタップするとA⇨Item1,Item2,Item3,Bをタップすると B⇨Item1,Item2,Item3というように各々セルがでてくるようにしています。その際、コードはdidSelectRowAtIndexPathメソッドに書いてあります。 そこで、次にItemのセルをタップすると、そのタップしたItemに相当する番号(プロパティリストを読み込む)に合わせて画面が遷移するようにしたいのですが、その方法がわかりません。 (didSelectRowAtIndexPathのところに書くと、セルのAやBをタップした時点で画面が遷移してしまい、Itemのセルがでてこないです。) 色々試してはいるのですが、どのように書いたらいいか、わからなくています。 もし、宜しければ、お手数おかけしますがご教授頂けたら幸いです。 以下、コードです。 Xcodeは5.0を使用しています。 @property (nonatomic, retain) NSArray *arrayOriginal; @property (nonatomic, retain) NSMutableArray *arForTable; //ここで、プロパティリストを読み込んでいます。 NSDictionary *dTmp=[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data1" ofType:@"plist"]]; self.arrayOriginal=[dTmp valueForKey:@"Objects"]; self.arForTable=[[NSMutableArray alloc] init] ; [self.arForTable addObjectsFromArray:self.arrayOriginal]; - (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] ; } cell.textLabel.text=[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"name"]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row]; if([d valueForKey:@"Objects"]) { NSArray *ar=[d valueForKey:@"Objects"]; BOOL isAlreadyInserted=NO; for(NSDictionary *dInner in ar ){ NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner]; isAlreadyInserted=(index>0 && index!=NSIntegerMax); if(isAlreadyInserted) break; } if(isAlreadyInserted) { //miniMizeThisRowでAやBが閉じるようにしています。 [self miniMizeThisRows:ar]; } else { NSUInteger count=indexPath.row+1; NSMutableArray *arCells=[NSMutableArray array]; for(NSDictionary *dInner in ar ) { [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]]; [self.arForTable insertObject:dInner atIndex:count++]; } //ここでAやBが開いて相当するItemがでるようにしています。 [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationFade]; } } //閉まる際のメソッドです。 -(void)miniMizeThisRows:(NSArray*)ar{ for(NSDictionary *dInner in ar ) { NSUInteger indexToRemove=[self.arForTable indexOfObjectIdenticalTo:dInner]; NSArray *arInner=[dInner valueForKey:@"Objects"]; if(arInner && [arInner count]>0){ [self miniMizeThisRows:arInner]; } if([self.arForTable indexOfObjectIdenticalTo:dInner]!=NSNotFound) { [self.arForTable removeObjectIdenticalTo:dInner]; [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: [NSIndexPath indexPathForRow:indexToRemove inSection:0] ] withRowAnimation:UITableViewRowAnimationFade]; }}} プロパティリストです。 <dict> <key>Objects</key> <array> <dict> <key>name</key> <string>A</string> <key>level</key> <integer>0</integer> <key>Objects</key> <array> <dict> <key>name</key> <string>Item1</string> <key>level</key> <integer>3</integer> </dict> <dict> <key>name</key> <string>Item2</string> <key>level</key> <integer>1</integer> </dict> <dict> <key>name</key> <string>Item3</string> <key>level</key> <integer>1</integer> </dict> </array> </dict> <dict> <key>name</key> <string>B</string> <key>level</key> <string></string> <key>Objects</key> <array> <dict> <key>name</key> <string>Item1</string> <key>level</key> <integer>0</integer> </dict> <dict> お手数おかけしまして恐縮ですが、宜しくお願い致します。

  • xcodeでのiOS開発で、コードで画面遷移する時

    xcodeでのiOS開発で、コードで画面遷移する方法について質問です。 テーブルビューで、セルをタップすると呼び出されるメソッドで コードで画面遷移をさせようとしています。 しかし、画面遷移をすると遷移先の画面が真っ黒になってしまいます。 どうすればなおりますか? - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NextViewController *nvc = [[NextViewController alloc] init]; nvc.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:nvc animated:YES]; } ちなみに、遷移先のクラスはインポートしてあります。 StoryBoardは使っていません。

専門家に質問してみよう