简体   繁体   中英

Adding a Navigation Controller to a Tab Bar Application (Programmatically)

This might look like a silly question to some, but handling the different types of controllers in an iPhone application is still a little fuzzy to me. Here's the setup:

I have a Tab Bar application with four tabs. Each tab passes control to its respective ViewController, where some of those are initialized with a .XIB file and some are done purely programmatically. One of the programmatic ones is DirectionsViewController , which is essentially a UITableViewController . Selecting a cell from its table needs to present (modally) a DetailedDirectionsViewController , which needs to have some sort of back-reference to the presenting view controller. I figured the easiest way to do this is to add a navigation controller to the Directions and DetailedDirections VCs - except I don't know how to do this without a .XIB file.

Also, the way I hand control over to DetailedDirections is by changing Directions the following way:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailedDirectionsViewController *vc = [[DetailedDirectionsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    [self.tabBarController presentModalViewController:vc animated:YES];
}

I seem to recall one of my professors saying that presentModalViewController is kind of an old method and there are better alternatives... I just can't remember them right now.

For what you want to do it would be best to have the tab in your tabbar manage a UINavigationController, and to set the rootViewController of that navigation controller to your DirectionsViewController.

Then in your direction view controller's didSelectRowAtIndexPath: methods you can do the following:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailedDirectionsViewController *vc = [[DetailedDirectionsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    [self.navigationController pushViewController:vc animated:YES];
}

And it will function like you want it to. The UINavigation controller will take care of putting a back button on your detailed directions view controller.

如果我理解得很好,则只需在tableview:didSelectRow ..:中创建一个导航控制器,然后使用要模态显示的视图控制器对其进行初始化,然后将其呈现出来,创建一个UIBarButtonItem并将其添加到导航的导航栏中控制器作为选择器,在内部使用dismiss命令创建一个新方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM