2017-01-28 4 views
1

私は現在、テーブルビューの行を選択して戻ってナビバーの「+」ボタンを押すと、アプリケーションがクラッシュするという問題に直面しています。私はxcodeで "show modally"オプションを選択したため、これが原因である可能性があります。とにかく、私は完全にオフかもしれませんが、ここに私のコードです。私は、「+」ボタンをクリックした場合ので、これは私のprepareForSegue機能であると私はpath.rowするvc.pIDを設定しようとすると、エラーが起こっているナビゲーションバー "キャンセル"

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    NSIndexPath *path = [self.tableView indexPathForSelectedRow]; 
    displayViewController *vc; 

    vc = [segue destinationViewController]; 
    if (path != NULL) { 
     vc.pID = path.row; 
    } 
} 

をコーディングするだけで拾ってきたように私と一緒にご負担願います、それは行を持っていません。 "+"ボタンを押すと、prepareForSegue関数に入りませんか?

ありがとうございました!

+0

、パスがゼロでなければなりません。どのようにvc.pIDを割り当てるのですか? – jesse

答えて

1

Objective-Cの中セグエでの準備の形...

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"someIdentifierA"]) { 
     // get the table view selection, for example 
     NSIndexPath *path = [self.tableView indexPathForSelectedRow]; 
     // get some part of my datasource (model), for example 
     MyModelObject *modelObject = self.datasourceArray[indexPath.row]; 
     // initialize part of the model on the destination vc 
     MyOtherViewController *destination = (MyOtherViewController *)segue.destinationViewController; 
     destination.modelObject = modelObject; 
    } else if ([segue.identifier isEqualToString:@"someIdentifierB"]) { 
     // same idea here, but different segue to different destination 
     NSIndexPath *path = [self.tableView indexPathForSelectedRow]; 
     displayViewController *vc = (displayViewController *)[segue destinationViewController]; 
     vc.pID = path.row; 
    } else if // and so on 
    } 
} 
+0

これは私の問題を解決してくれてありがとう! – Tim

1

あなたがしたいのは、segue識別子のテストをprepareForSegueに追加することです。

segueプロパティには識別子値があります。

if segue.identifier == "nameOfSegueIdentifier" { // do stuff here } 

このようにして、UIのどの部分がセグをトリガしているかによって、特定のコードを実行できます。

編集

申し訳ありませんが、あなただけは客観C.私のコードを使用していることスウィフトであることを今見ました。目的Cでの実装方法は不明ですが、概念は同じです。あなたが選択した行を持っていない場合は

関連する問題