建工程,建一个类WebViewController 继承于UIViewController
WebViewController设置为根视图控制器
WebViewController遵守UIWebViewDelegate协议以便网页视图加载或停止加载,加载出错时或执行什么方法,发生什么事件
WebViewController.m中代码
#import "WebViewController.h" @interface WebViewController ()
{
UIWebView *_webView;
UITextField *_textField;
UIActivityIndicatorView *_activityIndicatorView;
}
@end @implementation WebViewController
-(void)dealloc
{
[_webView release];
[_textField release];
[_activityIndicatorView release];
[super dealloc];
}
//webView获取url地址
- (void)loadWebPageWithString:(NSString *)urlstring
{
NSURL *url = [NSURL URLWithString:urlstring];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
}
//点击按钮,网页加载
- (void)buttonPress:(id)sender
{
[_textField resignFirstResponder];
[self loadWebPageWithString:_textField.text];
}
//代理中的方法
-(void)webViewDidStartLoad:(UIWebView *)webView
{
//网页加载时 进度轮开始启动
[_activityIndicatorView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//网页加载完成 进度轮停止
[_activityIndicatorView stopAnimating];
}
//请求页面出现错误:
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网址输入错误或网络断开" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
[alertView release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
_textField = [[UITextField alloc] initWithFrame:CGRectMake(, , , )];
_textField.borderStyle = UITextBorderStyleRoundedRect;
//输入框默认显示http://www.baidu.com
_textField.text = @"http://www.baidu.com";
//点击button进入网页
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(, , , );
[button setTitle:@"GO" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
//网页显示
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(, , , )];
_webView.delegate = self;
[self.view addSubview:_webView];
[self.view addSubview:_textField];
[self.view addSubview:button];
//设置进度轮
_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(, , , )];
//进度轮中心位置
[_activityIndicatorView setCenter:self.view.center];
//进度轮显示类型
[_activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
[self.view addSubview:_activityIndicatorView];
//没有点击按钮时,执行方法进入默认显示网页
[self buttonPress:nil]; // Do any additional setup after loading the view.
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end
实例代码:LiSWebView.zip