zoukankan      html  css  js  c++  java
  • UIWebView的使用,简单浏览器的实现



    #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

  • 相关阅读:
    触发器
    突然的感慨
    最近接手一个asp老项目,运行了4,5年了
    导出sql语句相关问题
    PHP入门速成
    统一项目时间格式(DateTime.ParseExact使用)
    想法太多了就是三脚猫。
    个人习惯培养计划(转):以此为诫,努力提高自身素质。
    excel处理函数打包
    ASP.Net MVC探索之路 增加字符串长度范围校验Attribute
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5347544.html
Copyright © 2011-2022 走看看