我们在工具条中追加4个按钮,触摸后依次调用goBack方法,goForward方法,reload方法,stopLoading方法。,基本上能够实现简单的浏览器的功能,最主要的还是利用了UIWebView的知识点,现在我们会发现以前感觉很牛X的东西,其实是那么的简单啊。,呵呵,你是否又增加了写自信。
下面我给大家分分享一下代码,大家如果有不懂的,看看官方文档,或者百度一下,就知道了
代码如下:
HHLAppDelegate.h
#import
@class HHLViewController;
@interface HHLAppDelegate : UIResponder
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) HHLViewController *viewController;
@property (strong,nonatomic) UINavigationController *myNaVC;
@end
#import "HHLAppDelegate.h"
#import "HHLViewController.h"
@implementation HHLAppDelegate
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[HHLViewController alloc] initWithNibName:@"HHLViewController" bundle:nil] autorelease];
UINavigationController *pNaVC = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.myNaVC = pNaVC;
[pNaVC release];
self.window.rootViewController = self.myNaVC;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
#import
@interface HHLViewController : UIViewController
{
@private
UIWebView *myWebView;
//UIActivityIndicatorView *myIndicatorView;
UIBarButtonItem *reloadButton;
UIBarButtonItem *stopButton;
UIBarButtonItem *backbutton;
UIBarButtonItem *forwardButton;
}
@end
#import "HHLViewController.h"
@interface HHLViewController ()
@end
@implementation HHLViewController
- (void)dealloc
{
//[myIndicatorView release];
if (myWebView.loading) {
[myWebView stopLoading];
}
myWebView.delegate = nil;//苹果文档中推荐,release前需要如此编写
[myWebView release];
[reloadButton release];
[stopButton release];
[backbutton release];
[forwardButton release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"UIWebView测试";
myWebView = [[UIWebView alloc]init];
myWebView.delegate =self;
myWebView.frame = self.view.bounds;
myWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
myWebView.scalesPageToFit = YES;//将UIWebView的scalePageToFit属性设置为YES,这样Web页面就会根据屏幕大小自动伸缩。
[self.view addSubview:myWebView];
[self.navigationController setNavigationBarHidden:NO animated:YES];
[self.navigationController setToolbarHidden:NO animated:YES];
//在工具条中追加活动指示器
//myIndicatorView = [[UIActivityIndicatorView alloc]init];
//myIndicatorView.frame =CGRectMake(0, 0, 50, 50);
//UIBarButtonItem *indicator = [[[UIBarButtonItem alloc] initWithCustomView:myIndicatorView]autorelease];
//UIBarButtonItem *adjustment = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]autorelease];
//NSArray *buttons = [NSArray arrayWithObjects:adjustment,indicator,adjustment, nil];
//[self setToolbarItems:buttons animated:YES];
//在工具条中添加按钮
reloadButton =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reloadDidPush)];
stopButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(stopDidPush)];
backbutton = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backDidPush)];
forwardButton = [[UIBarButtonItem alloc]initWithTitle:@"Forward" style:UIBarButtonItemStyleBordered target:self action:@selector(forwardDidPush)];
NSArray *buttons = [NSArray arrayWithObjects:backbutton,forwardButton,reloadButton,stopButton, nil];
[self setToolbarItems:buttons animated:YES];
}
- (void)reloadDidPush
{
[myWebView reload];//重新载入页面
}
- (void)stopDidPush
{
if (myWebView.loading) {
[myWebView stopLoading];//停止读入
}
}
- (void)backDidPush
{
if (myWebView.canGoBack) {
[myWebView goBack];//返回前一页面
}
}
- (void)forwardDidPush
{
if (myWebView.canGoForward) {
[myWebView goForward];//进入到下一页面
}
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//Web页面显示
NSURLRequest *myRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com/"]];
[myWebView loadRequest:myRequest];
[self updateViewConstraints];
}
- (void)viewWillDisappear:(BOOL)animated
{
//画面关闭时状态条的活动提示器设置为OFF
[super viewWillDisappear:animated];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
// [myIndicatorView startAnimating];
[self updateViewConstraints];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
// [myIndicatorView stopAnimating];
[self updateViewConstraints];
}
//
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
// [myIndicatorView stopAnimating];
[self updateViewConstraints];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end