2017-08-16 9 views
0

私はタブベースのアプリケーションを持っています。 SecondViewControllerには、複数のセルを持つUITableViewがあります。セルをスワイプすると、別のビューコントローラspeciesControllerに送信されます。以下のコードは、別のビューにアプリケーションを送信するために動作し、ナビゲーションバーを設定しますが、戻るボタンはありません。Self.NaviationControllerを初期化する

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    SpeciesViewController* speciesController = [[SpeciesViewController alloc]initWithNibName:@"SpeciesViewController" bundle:nil]; 


    Species *theSpecies = [fetchedResultsController objectAtIndexPath:indexPath]; 
    speciesController.theSpecies = theSpecies; 

    switch (sortBySegmentedControl.selectedSegmentIndex) { 
     case kSortByCommonNameFirst: 
      speciesController.title = [theSpecies commonNameFirstLast]; 
      break; 
     case kSortByCommonNameLast: 
      speciesController.title = [theSpecies commonNameLastFirst]; 
      break; 
     case kSortByScientificName: 
      speciesController.title = [[NSString alloc] initWithFormat:@"%@%@", 
             [theSpecies.scientificName substringToIndex:1], 
             [[theSpecies.scientificName substringFromIndex:1] lowercaseString]]; 
      break; 
     default: 
      break; 
    } 

    speciesController.hidesBottomBarWhenPushed = YES; 
    //self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; 

    //// Store current index path for viewDidAppear animation. //// 
    self->currentSelectedIndexPath = indexPath; 
    UINavigationController *navBar=[[UINavigationController alloc]initWithRootViewController:speciesController]; 
    navBar.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; 
    [self presentViewController:navBar animated:YES completion:nil]; 

} 

私は戻るボタンを持っていることを実現する、あなたはナビゲーションコントローラのスタックに現在のビューをプッシュする必要があります。しかし、

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; 
[self.navigationController pushViewController:navBar animated:NO]; 

navBar.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; 
[self presentViewController:navBar animated:YES completion:nil]; 

を変更することはできません。実際には、アプリが別のビューに変更するには失敗した、と私はself.navigationController

私は初期化/この行と1追加しようとしましたが存在しないため、これがあると信じて:

UINavigationController *navigationController=[[UINavigationController alloc]initWithRootViewController:self]; 

をしかし、それはアプリケーションを引き起こしましたクラッシュする

ナビゲーションコントローラーをアプリに追加する方法については、ストーリーボードまたはプログラムでご理解いただきますようお願い申し上げます。私が見た例のほとんどは、2011年以降の方法で古くなっていたようです。self.window addSubview...

ありがとう!

答えて

0

を試し、そのルートVCとしてSecondViewControllerとナビゲーションコントローラにそのタブ項目を設定します。

その後、didSelectRowAtIndexPathに、あなたがしなければならないすべては、次のとおりです。

[self.navigationController pushViewController: speciesController animated:YES]; 

「戻るボタン」に自動的に存在します。

+0

ありがとう – Matt

0

UINavigationController *navBar=[[UINavigationController alloc]initWithRootViewController:speciesController]; 

後代わりにタブ項目としてSecondViewControllerを有するので

speciesController.navigationController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; 
if (self.navigationController) 
    [self.navigationController pushViewController:navBar animated:NO]; 
else 
    [self presentViewController: navBar animated:YES completion:nil]; 
+0

'キャッチされていない例外 'NSInvalidArgumentException'のためアプリを終了しています、理由: 'アクティブなコントローラをモーダルに表示しようとしました' – Matt