zoukankan      html  css  js  c++  java
  • 对UIWebView的学习

    建工程,建一个类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(10, 20, 270, 30)];
        _textField.borderStyle = UITextBorderStyleRoundedRect;
        //输入框默认显示http://www.baidu.com
        _textField.text = @"http://www.baidu.com";
        //点击button进入网页
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(280, 20, 30, 30);
        [button setTitle:@"GO" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
        //网页显示
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 50, 320, 425)];
        _webView.delegate = self;
        [self.view addSubview:_webView];
        [self.view addSubview:_textField];
        [self.view addSubview:button];
        //设置进度轮
        _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
        //进度轮中心位置
        [_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

  • 相关阅读:
    15:链表中倒数第K个节点
    14:调整数组顺序使奇数位于偶数的前面
    13:在O(1)时间内删除单向链表中的一个节点
    centos7下zookeeper集群安装部署
    zabbix3.2监控mysql
    解决关于confluence缓慢 字体乱码 宏乱码 编辑不能贴图等问题
    nginx日志文件的定时切割与归纳
    centos7安装sonarqube6.7 代码质量管理平台
    centos7下安装vnc更改vnc默认端口号
    centos7安装java环境和maven环境
  • 原文地址:https://www.cnblogs.com/limicheng/p/3871479.html
Copyright © 2011-2022 走看看