新建一个基于singleView的工程,名为webView。
在ViewController.xib中添加两个按钮,并分别关联两个方法openDoc,openWeb。
新建一个objective-c class用来展现doc或者网页的内容。
放一个web View控件和一个返回按钮。
修改WebViewController.h
//
// WebViewController.h
// webView
//
// Created by cloud on 13-3-27.
// Copyright (c) 2013年 cloud. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface WebViewController :UIViewController
{
NSURL *_theURL;
IBOutlet UIWebView *_webView;
}
- (IBAction)pressBack:(UIButton *)sender;
@property (nonatomic,retain)NSURL *theURL;
@end
修改ViewController.m- (IBAction)openDoc:(UIButton *)sender {
WebViewController *web=[[[WebViewControlleralloc] init] autorelease];
web.theURL=[NSURLfileURLWithPath:[[NSBundlemainBundle] pathForResource:@"健身"ofType:@"doc"]];
[selfpresentModalViewController:web animated:YES];
}
- (IBAction)openWeb:(UIButton *)sender {
WebViewController *web=[[[WebViewControlleralloc] init] autorelease];
web.theURL=[NSURLURLWithString:@"http://www.sohu.com"];
[selfpresentModalViewController:web animated:YES];
}
修改webViewController.m- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view from its nib.
NSURLRequest *req=[NSURLRequestrequestWithURL:_theURL];
[_webViewloadRequest:req];
}
添加UIWebViewDelegate,处理失败事件- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
UIAlertView *alert=[[[UIAlertViewalloc] initWithTitle:@"警告"message:[error description]delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:@"不确定",nil] autorelease ];
[alertshow];
}
ViewController.xib中新加一个按钮,测试警告框。- (IBAction)pressAlert:(UIButton *)sender {
// UIAlertView *alert=[[[UIAlertView alloc] initWithTitle:@"警告" message:@"出错了" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"不确定", nil] autorelease ];
// [alert show];
UIActionSheet *as=[[UIActionSheetalloc] initWithTitle:@"加载失败" delegate:nilcancelButtonTitle:@"取消"destructiveButtonTitle:@"确定"otherButtonTitles:@"张三",@"李四",@"王五",nil];
[asshowInView:self.view];
}
源码地址:http://download.csdn.net/detail/cloud95/5187232