#import <UIKit/UIKit.h> // 模板默认引入包含程序需要使用“类”的框架,即 Foundation.h头文件,使它包含在程序中 #import <Foundation/Foundation.h> // UIViewController类为程序提供了基本的视图管理模块。 @interface NavControllerViewController : UIViewController { // 把所需要显示的视图告诉Interface Builder IBOutlet UIWebView *webView; } // 此@property声明程序的属性,加入在@interface中指定的相同webView; @property(nonatomic, retain) UIWebView *webView; @end
#import "NavControllerViewController.h" @interface NavControllerViewController () @end @implementation NavControllerViewController // 本指令告诉编译器去合成今天方法所需要的“网页视图”控件的存储方法 @synthesize webView; // 构建在网页视图中打开指定网站的按钮方法 -(IBAction)openMaps{ NSString *addressText = @"tian an men, beijing, china"; // 在字符串程序的数据中把字符转化为URL网页格式。 // 此处NSASCIIStringEncoding中使用ASCII为字符串的格式转换 addressText = [addressText stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; // urlText是字符定义所创建的文字对象,处理指定的谷歌中国地图网站。 // 其中addressText是对网站发送一个指定的地址 NSString *urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@",addressText]; // 此处开启网页视图与用户交互属性值 webView.userInteractionEnabled = true; // 网页视图向网站发送一个指定网站内容的urlText数据变量 [webView loadRequest:[[NSURLRequest alloc] initWithURL :[[NSURL alloc]initWithString:urlText] ]]; } - (void)viewDidLoad { [webView release]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } // 释放可用内存供给应用程序,并提早发出警告提示 - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
第三步,在XIB页面拖放两个控件,一个WebView和一个Button。
然后按住 control 后,点击File's Owner一直拖到WebView控件上面,然后选中webView;
然后按住 control 后,点击Button一直拖到File's Owner上面,然后选中openMaps;
完了。