Matt Matt - 6 months ago 26
iOS Question

Initialize Self.NaviationController

I have a tab-based application. In

SecondViewController
, there is a
UITableView
with several cells. When you swipe on a cell, you are sent to another view controller,
speciesController
. The below code works to send the app to another view, and it does set up a navigation bar, but there is no back button.

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

}


I realize that to have a backbutton, you need to push the current view onto the navigation controller stack. However, changing

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


to

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


Doesn't work. In fact, the app fails to change to a different view, and I believe this is because there is no
self.navigationController


I tried to add/initialize one with this line:

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


but that caused the application to crash.

I appreciate any information about adding a navigation controller to an app (in storyboard or programatically). I'm afraid most of the examples I saw were outdated, from 2011 or so with methods like
self.window addSubview...


Thank you!

Answer Source

Instead of having SecondViewController as a Tab Item, set that Tab Item to a Navigation Controller with SecondViewController as its Root VC.

Then, on didSelectRowAtIndexPath, all you have to do is:

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

The "back button" will automatically be there.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download