1 @implementation RootViewController 2 3 - (void)viewDidLoad 4 { 5 [super viewDidLoad]; 6 _demoNameArray = [[NSArray alloc]initWithObjects: 7 @"基本地图功能-MapViewBaseDemo", 8 @"多地图使用功能-MultiMapViewDemo", 9 @"图层展示功能-MapViewDemo", 10 @"地图操作功能-MapViewControlDemo", 11 @"UI控制功能-MapViewUISettingDemo", 12 @"定位功能-LocationDemo", 13 @"覆盖物功能-AnnotationDemo", 14 @"自定义绘制-CustomOverlayDemo", 15 @"POI搜索功能-PoiSearchDemo", 16 @"地理编码功能-GeocodeDemo", 17 @"路径规划功能-RouteSearchDemo", 18 @"公交线路查询-BusLineSearch", 19 @"离线地图功能-OfflineDemo", 20 @"短串分享功能-ShortUrlShareDemo", 21 @"云检索功能-CloudSearchDemo", 22 @"调启百度导航功能-NavigationDemo", 23 nil]; 24 25 NSMutableArray *array = [[NSMutableArray alloc] init]; 26 MapViewBaseDemoViewController *mapViewBaseDemoViewController = [[[MapViewBaseDemoViewController alloc] init] autorelease]; 27 mapViewBaseDemoViewController.title = @"基本地图功能"; 28 MultiMapViewDemo *multiMapViewDemo = [[[MultiMapViewDemo alloc] init] autorelease]; 29 multiMapViewDemo.title = @"多地图使用功能"; 30 MapViewDemoViewController *mapviewDemoController = [[[MapViewDemoViewController alloc] init] autorelease]; 31 mapviewDemoController.title = @"图层展示功能"; 32 MapViewControlDemoViewController *mapviewControlDemoController = [[[MapViewControlDemoViewController alloc] init] autorelease]; 33 mapviewControlDemoController.title = @"地图操作功能"; 34 35 MapViewUISettingDemoViewController *mapViewUISettingDemoViewController = [[[MapViewUISettingDemoViewController alloc] init] autorelease]; 36 mapViewUISettingDemoViewController.title = @"UI控制功能"; 37 38 LocationDemoViewController *locationDemoController = [[[LocationDemoViewController alloc] init] autorelease]; 39 locationDemoController.title = @"定位功能"; 40 41 AnnotationDemoViewController *annotationDemoController = [[[AnnotationDemoViewController alloc] init] autorelease]; 42 annotationDemoController.title = @"覆盖物功能"; 43 44 CustomOverlayDemoViewController *customOverlayDemo = [[[CustomOverlayDemoViewController alloc] init] autorelease]; 45 customOverlayDemo.title = @"自定义绘制"; 46 47 PoiSearchDemoViewController *poiSearchDemoController = [[[PoiSearchDemoViewController alloc] init] autorelease]; 48 poiSearchDemoController.title = @"POI搜索功能"; 49 50 GeocodeDemoViewController *geocodeDemoController = [[[GeocodeDemoViewController alloc] init] autorelease]; 51 geocodeDemoController.title = @"地理编码功能"; 52 53 RouteSearchDemoViewController *routeSearchDemoController = [[[RouteSearchDemoViewController alloc] init] autorelease]; 54 routeSearchDemoController.title = @"路径规划功能"; 55 56 BusLineSearchViewController *buslineDemoViewController = [[[BusLineSearchViewController alloc] init] autorelease]; 57 buslineDemoViewController.title = @"公交线路查询"; 58 59 OfflineDemoViewController *offlineDemoViewController = [[[OfflineDemoViewController alloc] init] autorelease]; 60 offlineDemoViewController.title = @"离线地图功能"; 61 62 ShortUrlShareDemoViewController *shortUrlShareDemoViewController = [[[ShortUrlShareDemoViewController alloc] init] autorelease]; 63 shortUrlShareDemoViewController.title = @"短串分享功能"; 64 65 CloudSearchDemoViewController *cloudSearchDemoViewController = [[[CloudSearchDemoViewController alloc] init] autorelease]; 66 cloudSearchDemoViewController.title = @"云检索功能"; 67 68 NavigationDemoViewController *navigationDemoViewController = [[[NavigationDemoViewController alloc] init] autorelease]; 69 navigationDemoViewController.title = @"调启百度导航功能"; 70 71 [array addObject:mapViewBaseDemoViewController]; 72 [array addObject:multiMapViewDemo]; 73 [array addObject:mapviewDemoController]; 74 [array addObject:mapviewControlDemoController]; 75 [array addObject:mapViewUISettingDemoViewController]; 76 [array addObject:locationDemoController]; 77 [array addObject:annotationDemoController]; 78 [array addObject:customOverlayDemo]; 79 [array addObject:poiSearchDemoController]; 80 [array addObject:geocodeDemoController]; 81 [array addObject:routeSearchDemoController]; 82 [array addObject:buslineDemoViewController]; 83 [array addObject:offlineDemoViewController]; 84 [array addObject:shortUrlShareDemoViewController]; 85 [array addObject:cloudSearchDemoViewController]; 86 [array addObject:navigationDemoViewController]; 87 88 _viewControllerArray = array; }
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController* viewController = [_viewControllerArray objectAtIndex:indexPath.section];
UIBarButtonItem *customLeftBarButtonItem = [[UIBarButtonItem alloc] init];
customLeftBarButtonItem.title = @"返回";
self.navigationItem.backBarButtonItem = customLeftBarButtonItem;
[customLeftBarButtonItem release];
[self.navigationController pushViewController:viewController animated:YES];
}
以上是百度地图中demo的一点代码,是实现的功能是在一个tableview中点击不同的cell,就会转跳到不同的控制器,一显示百度地图不同的功能!如下图所示:
觉得这段代码有点冗余,所以就想优化一下,下面是自己的优化后的代码!
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 _demoNameArray = [[NSArray alloc]initWithObjects: 5 @"基本地图功能-MapViewBaseDemo", 6 @"多地图使用功能-MultiMapViewDemo", 7 @"图层展示功能-MapViewDemo", 8 @"地图操作功能-MapViewControlDemo", 9 @"UI控制功能-MapViewUISettingDemo", 10 @"定位功能-LocationDemo", 11 @"覆盖物功能-AnnotationDemo", 12 @"自定义绘制-CustomOverlayDemo", 13 @"POI搜索功能-PoiSearchDemo", 14 @"地理编码功能-GeocodeDemo", 15 @"路径规划功能-RouteSearchDemo", 16 @"公交线路查询-BusLineSearch", 17 @"离线地图功能-OfflineDemo", 18 @"短串分享功能-ShortUrlShareDemo", 19 @"云检索功能-CloudSearchDemo", 20 @"调启百度导航功能-NavigationDemo", 21 nil]; 22 23 24 [self setupViewController:[MapViewBaseDemoViewController class] title:@"基本地图功能"]; 25 [self setupViewController:[MultiMapViewDemo class] title:@"多地图使用功能"]; 26 [self setupViewController:[MapViewDemoViewController class] title:@"图层展示功能"]; 27 [self setupViewController:[MapViewControlDemoViewController class] title:@"地图操作功能"]; 28 [self setupViewController:[MapViewUISettingDemoViewController class] title:@"UI控制功能"]; 29 [self setupViewController:[LocationDemoViewController class] title:@"定位功能"]; 30 [self setupViewController:[AnnotationDemoViewController class] title:@"覆盖物功能"]; 31 [self setupViewController:[CustomOverlayDemoViewController class] title:@"自定义绘制"]; 32 [self setupViewController:[PoiSearchDemoViewController class] title:@"POI搜索功能"]; 33 [self setupViewController:[GeocodeDemoViewController class] title:@"地理编码功能"]; 34 [self setupViewController:[RouteSearchDemoViewController class] title:@"路径规划功能"]; 35 [self setupViewController:[BusLineSearchViewController class] title:@"公交线路查询"]; 36 [self setupViewController:[OfflineDemoViewController class] title:@"离线地图功能"]; 37 [self setupViewController:[ShortUrlShareDemoViewController class] title:@"短串分享功能"]; 38 [self setupViewController:[CloudSearchDemoViewController class] title:@"云检索功能"]; 39 [self setupViewController:[NavigationDemoViewController class] title:@"调启百度导航功能"]; 40 } 41 42 43 - (void)setupViewController:(Class)controller title:(NSString *)title 44 { 45 46 UIViewController *viewController = [[[controller alloc] init] autorelease]; 47 48 viewController.title = title; 49 50 [self.viewControllerArray addObject:viewController]; 51 52 } 53 54 #pragma mark - 55 #pragma mark Table view delegate 56 57 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 58 UIViewController* viewController = [self.viewControllerArray objectAtIndex:indexPath.section]; 59 UIBarButtonItem *customLeftBarButtonItem = [[UIBarButtonItem alloc] init]; 60 customLeftBarButtonItem.title = @"返回"; 61 self.navigationItem.backBarButtonItem = customLeftBarButtonItem; 62 [customLeftBarButtonItem release]; 63 [self.navigationController pushViewController:viewController animated:YES]; 64 65 }
时间: 2024-10-13 15:37:54