初始化 Self.NaviationController

Initialize Self.NaviationController

我有一个基于选项卡的应用程序。在 SecondViewController 中,有一个 UITableView 有几个单元格。当你在一个单元格上滑动时,你会被发送到另一个视图控制器,speciesController。下面的代码用于将应用程序发送到另一个视图,它确实设置了一个导航栏,但没有后退按钮。

- (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];

}

我意识到要有一个后退按钮,您需要将当前视图推送到导航控制器堆栈上。然而,改变

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];

无效。事实上,应用程序无法更改为不同的视图,我相信这是因为没有 self.navigationController

我试着 add/initialize 一个用这条线:

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

但这导致应用程序崩溃。

我很欣赏有关将导航控制器添加到应用程序(在故事板中或以编程方式)的任何信息。恐怕我看到的大多数示例都已过时,从 2011 年左右开始使用 self.window addSubview...

之类的方法

谢谢!

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

尝试

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];

不要将 SecondViewController 作为选项卡项,而是将该选项卡项设置为以 SecondViewController 作为其根的导航控制器 VC。

那么,在didSelectRowAtIndexPath,你所要做的就是:

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

"back button" 会自动出现。