2017-08-16 1 views
0

Ich habe eine tab-basierte Anwendung. In SecondViewController gibt es eine UITableView mit mehreren Zellen. Wenn Sie über eine Zelle wischen, werden Sie an einen anderen Ansichtscontroller gesendet, speciesController. Der folgende Code funktioniert, um die App an eine andere Ansicht zu senden, und sie richtet eine Navigationsleiste ein, aber es gibt keine Zurück-Schaltfläche.Initialize 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]; 

} 

Mir ist klar, dass ein Backbutton haben, können Sie die aktuelle Ansicht auf den Navigationscontroller Stapel schieben müssen. Allerdings ändert

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

zu

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

funktioniert nicht. In der Tat, schlägt die App zu einer anderen Ansicht zu ändern, und ich glaube, das ist, weil es keine self.navigationController ist

ich hinzufügen wollte/initialisieren ein mit dieser Zeile:

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

aber das verursacht die Anwendung zerstören.

Ich freue mich über alle Informationen zum Hinzufügen eines Navigations-Controllers zu einer App (im Storyboard oder programmatisch). Ich fürchte, die meisten der Beispiele, die ich sah, waren veraltet, ab 2011 oder so mit Methoden wie self.window addSubview...

Vielen Dank!

Antwort

0

Statt SecondViewController als Tab Artikel zu haben, festgelegt, dass Tab Artikel auf eine Navigation Controller mit SecondViewController als Root-VC.

Dann auf didSelectRowAtIndexPath, alles, was Sie tun müssen, ist:

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

Die "Zurück-Taste" es automatisch sein wird.

+0

perfekt, danke – Matt

0

nach

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

versuchen

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

'Beenden App aufgrund der nicht abgefangenen Ausnahme 'NSInvalidArgumentException', Grund: 'Anwendung versucht, modal einen aktiven Controller zu präsentieren' – Matt