1。新建一个基于Navigation-based Application的工程。
2。修改原来的RootViewController.h,RootViewController.m,RootViewController.xib为MyTableViewController.h,MyTableViewController.m,MyTableViewController.xib。
3。点击MainVindow.xib,将Rot View Controller的class设置为:MyTableViewController。
4。新建文件:DetailViewController.m文件并选择自动生成.h与.xib文件,然后在DetailViewController.xib拖入一个map view控件。
5。代码:(控件与属性连接就省略了)
第一个页面的代码:
MyTableViewController.h
- #import <UIKit/UIKit.h>
- @interface MyTableViewController : UITableViewController {
- NSMutableArray *listData;//表格第一部分的数据
- NSMutableArray * twolistData;//表格第二部分的数据
- }
- @property(nonatomic,retain) NSMutableArray *listData;
- @property(nonatomic,retain) NSMutableArray *twolistData;
- @end
#import <UIKit/UIKit.h>
@interface MyTableViewController : UITableViewController {
NSMutableArray *listData;//表格第一部分的数据
NSMutableArray * twolistData;//表格第二部分的数据
}
@property(nonatomic,retain) NSMutableArray *listData;
@property(nonatomic,retain) NSMutableArray *twolistData;
@end
MyTableViewController.m
- #import "MyTableViewController.h"
- #import "DetailViewController.h"
- @implementation MyTableViewController
- @synthesize listData;
- @synthesize twolistData;
- #pragma mark -
- #pragma mark View lifecycle
- //设置标题和初始化表格数据
- - (void)viewDidLoad {
- [super viewDidLoad];
- listData = [[NSMutableArray alloc] initWithObjects:@"Beijing",@"Shanghai",@"Guangzhou",nil];
- twolistData = [[NSMutableArray alloc] initWithObjects:@"Changsha", nil];
- self.title = @"第一个页面";
- }
- #pragma mark -
- #pragma mark Table view data source
- // 设置表格为两个部分
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 2;
- }
- //设置每个部分的标题
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- {
- NSString* secHeader = @"";
- if (section == 0)
- {
- secHeader = @"中国三大城市";
- }
- else if (section == 1)
- {
- secHeader = @"湖南省会";
- }
- return secHeader;
- }
- // 设置每个部分的显示行数
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- if (section == 0) {
- return listData.count;
- }
- else if (section == 1) {
- return twolistData.count;
- }
- return 0;
- }
- // 设置行数据
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
- }
- if (indexPath.section == 0) {
- cell.textLabel.text = [listData objectAtIndex:indexPath.row];
- }
- else if(indexPath.section == 1){
- cell.textLabel.text = [twolistData objectAtIndex:indexPath.row];
- }
- return cell;
- }
- #pragma mark -
- #pragma mark Table view delegate
- //表格行点击事件
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- NSString *selectedRow;
- if (indexPath.section == 0) {
- selectedRow = [listData objectAtIndex:indexPath.row];
- }else if(indexPath.section == 1){
- selectedRow = [twolistData objectAtIndex:indexPath.row];
- }
- DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
- detailViewController.selectedRow = selectedRow;
- [self.navigationController pushViewController:detailViewController animated:YES];
- [detailViewController release];
- }
- #pragma mark -
- #pragma mark Memory management
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- - (void)viewDidUnload {
- }
- - (void)dealloc {
- [listData release];
- [twolistData release];
- [super dealloc];
- }
- @end
#import "MyTableViewController.h"
#import "DetailViewController.h"
@implementation MyTableViewController
@synthesize listData;
@synthesize twolistData;
#pragma mark -
#pragma mark View lifecycle
//设置标题和初始化表格数据
- (void)viewDidLoad {
[super viewDidLoad];
listData = [[NSMutableArray alloc] initWithObjects:@"Beijing",@"Shanghai",@"Guangzhou",nil];
twolistData = [[NSMutableArray alloc] initWithObjects:@"Changsha", nil];
self.title = @"第一个页面";
}
#pragma mark -
#pragma mark Table view data source
// 设置表格为两个部分
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
//设置每个部分的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString* secHeader = @"";
if (section == 0)
{
secHeader = @"中国三大城市";
}
else if (section == 1)
{
secHeader = @"湖南省会";
}
return secHeader;
}
// 设置每个部分的显示行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return listData.count;
}
else if (section == 1) {
return twolistData.count;
}
return 0;
}
// 设置行数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section == 0) {
cell.textLabel.text = [listData objectAtIndex:indexPath.row];
}
else if(indexPath.section == 1){
cell.textLabel.text = [twolistData objectAtIndex:indexPath.row];
}
return cell;
}
#pragma mark -
#pragma mark Table view delegate
//表格行点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedRow;
if (indexPath.section == 0) {
selectedRow = [listData objectAtIndex:indexPath.row];
}else if(indexPath.section == 1){
selectedRow = [twolistData objectAtIndex:indexPath.row];
}
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
detailViewController.selectedRow = selectedRow;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[listData release];
[twolistData release];
[super dealloc];
}
@end
第二个页面的代码:
DetailViewController.h
- #import <UIKit/UIKit.h>
- #import <Foundation/Foundation.h>
- @interface DetailViewController : UIViewController {
- NSString *selectedRow;//用来保存前一个页面传过来的参数
- IBOutlet UIWebView *webView;//浏览器控件
- }
- @property (nonatomic,retain) NSString *selectedRow;
- @property (nonatomic,retain) UIWebView *webView;
- @end
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface DetailViewController : UIViewController {
NSString *selectedRow;//用来保存前一个页面传过来的参数
IBOutlet UIWebView *webView;//浏览器控件
}
@property (nonatomic,retain) NSString *selectedRow;
@property (nonatomic,retain) UIWebView *webView;
@end
DetailViewController.m
- #import "DetailViewController.h"
- @implementation DetailViewController
- @synthesize selectedRow,webView;
- //设置标题和根据传过来的参数打开google地图
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.title = selectedRow;//设置标题
- NSString* addressText = selectedRow;
- addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
- NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@",addressText];
- webView.userInteractionEnabled = true;
- [webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:urlText]]];//打开google地图
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- - (void)viewDidUnload {
- [super viewDidUnload];
- }
- - (void)dealloc {
- [super dealloc];
- [selectedRow release];
- [webView release];
- }
- @end
#import "DetailViewController.h"
@implementation DetailViewController
@synthesize selectedRow,webView;
//设置标题和根据传过来的参数打开google地图
- (void)viewDidLoad {
[super viewDidLoad];
self.title = selectedRow;//设置标题
NSString* addressText = selectedRow;
addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@",addressText];
webView.userInteractionEnabled = true;
[webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:urlText]]];//打开google地图
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
[selectedRow release];
[webView release];
}
@end