UIWebView的使用,简单浏览器的实现
2016-04-02 14:34
323 查看
#import "ViewController.h"
@interface ViewController () <UIWebViewDelegate>
@property(nonatomic,weak)UIWebView* web;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self _setTextField];
[self _loadWebView];
}
#pragma mark - 设置输入框
- (void)_setTextField{
self.tf.clearButtonMode=UITextFieldViewModeWhileEditing;
}
#pragma mark - 载入WebView
- (void)_loadWebView{
UIWebView* web=[[UIWebView alloc] init];
web.frame=CGRectMake(0,self.view.frame.size.height,self.view.frame.size.width,self.view.frame.size.height-42 );
self.web=web;
web.delegate=self;
[self.view addSubview:web];
}
#pragma mark - 放弃第一响应
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.tf resignFirstResponder];
}
#pragma mark - Action
- (IBAction)backAction:(id)sender {
[self.web goBack];
// self.web.frame=CGRectMake(0,self.view.frame.size.height,self.view.frame.size.width,self.view.frame.size.height-42 );
}
- (IBAction)forwardAction:(id)sender {
[self.web goForward];
self.web.frame=CGRectMake(0,20,self.view.frame.size.width,self.view.frame.size.height-50);
}
- (IBAction)undoAction:(id)sender {
[self.web stopLoading];
}
- (IBAction)refreshAction:(id)sender {
[self.web reload];
}
- (IBAction)searchAction:(id)sender {
[self.tf resignFirstResponder];
self.web.frame=CGRectMake(0,20,self.view.frame.size.width,self.view.frame.size.height-60);
[self _sendRequest];
}
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
// NSLog(@"%d===%d",[webView canGoBack],[webView canGoForward]);
self.backBt.enabled=[webView canGoBack];
self.forwardBt.enabled=[webView canGoForward];
self.refresh.enabled=([webView canGoBack] ||[webView canGoForward]);
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"%@",error);
}
- (void)_sendRequest{
//http://www.baidu.com
NSURL* url=[NSURL URLWithString:self.tf.text];
NSURLRequest* request=[NSURLRequest requestWithURL:url];
[self.web loadRequest:request];
}
@end
@interface ViewController () <UIWebViewDelegate>
@property(nonatomic,weak)UIWebView* web;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self _setTextField];
[self _loadWebView];
}
#pragma mark - 设置输入框
- (void)_setTextField{
self.tf.clearButtonMode=UITextFieldViewModeWhileEditing;
}
#pragma mark - 载入WebView
- (void)_loadWebView{
UIWebView* web=[[UIWebView alloc] init];
web.frame=CGRectMake(0,self.view.frame.size.height,self.view.frame.size.width,self.view.frame.size.height-42 );
self.web=web;
web.delegate=self;
[self.view addSubview:web];
}
#pragma mark - 放弃第一响应
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.tf resignFirstResponder];
}
#pragma mark - Action
- (IBAction)backAction:(id)sender {
[self.web goBack];
// self.web.frame=CGRectMake(0,self.view.frame.size.height,self.view.frame.size.width,self.view.frame.size.height-42 );
}
- (IBAction)forwardAction:(id)sender {
[self.web goForward];
self.web.frame=CGRectMake(0,20,self.view.frame.size.width,self.view.frame.size.height-50);
}
- (IBAction)undoAction:(id)sender {
[self.web stopLoading];
}
- (IBAction)refreshAction:(id)sender {
[self.web reload];
}
- (IBAction)searchAction:(id)sender {
[self.tf resignFirstResponder];
self.web.frame=CGRectMake(0,20,self.view.frame.size.width,self.view.frame.size.height-60);
[self _sendRequest];
}
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
// NSLog(@"%d===%d",[webView canGoBack],[webView canGoForward]);
self.backBt.enabled=[webView canGoBack];
self.forwardBt.enabled=[webView canGoForward];
self.refresh.enabled=([webView canGoBack] ||[webView canGoForward]);
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"%@",error);
}
- (void)_sendRequest{
//http://www.baidu.com
NSURL* url=[NSURL URLWithString:self.tf.text];
NSURLRequest* request=[NSURLRequest requestWithURL:url];
[self.web loadRequest:request];
}
@end
相关文章推荐
- Android Studio初次使用问题
- IOS 方法带可变参数写法及解释
- note(持续更新中)
- IOS中DES与MD5加密方案
- Android 自定义GridView,解决嵌套Grideview的显示不完全的问题
- Android初级教程_获取Android控件的宽和高
- iOS开发:开发中的一些小技巧
- swift segment实现页面切换
- Android SurfaceView类的使用
- Android Binder 简单理解
- Android OOM分析总结
- Android JSON数据解析
- Android XML数据解析
- cocos::触摸跟随移动
- 深入理解Objective-C:Category(下)
- Android仿淘宝订单页面实现
- 深入理解Objective-C:Category(上)
- Android studio下载依赖时提示连接超时time out的解决方法
- Android-->MediaMuxer,MediaCodec,AudioRecord及Camera实现音频视频混合MP4文件
- Object wait T ms to run again