UIWebView是ios开发中比较常用的一个控件。我们可以用它来浏览网页、打开文档等,今天笔者在这里简单介绍下UIWebView和UISearchBar结合起来的用法,做一个简单的类浏览器。

  一:首先定义这两个控件,并在.h文件中实现UISearchBarDelegate,UIWebViewDelegate两个代理

  @interface TestView : UIViewController<UISearchBarDelegate,UIWebViewDelegate>

  @property(nonatomic)UISearchBar* searchBar;

 @property(nonatomic,retain)UIWebView* webView;

  二:加载这两个控件

//加载searcBar
-(void)initSearchBar
{
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.size.width, )];
self.searchBar.delegate = self; //接受委托
self.searchBar.text = @"http://";
//UISearchBar上按钮的默认文字为Cancel,这里改为“GO”版本不同方法有些许区别
for(id cc in [self.searchBar subviews])
{
for (UIView *view in [cc subviews]) {
if ([NSStringFromClass(view.class) isEqualToString:@"UINavigationButton"])
{
UIButton *btn = (UIButton *)view;
[btn setTitle:@"GO" forState:UIControlStateNormal];
}
}
} [self.view addSubview:self.searchBar]; }
//加载webview
-(void)initWebView
{
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-)];
[self.webView setUserInteractionEnabled:YES]; //设置是否支持交互
[self.webView setDelegate:self]; //接受委托
[self.webView setScalesPageToFit:YES]; //设置自动缩放
[self.view addSubview:self.webView];
}

在viewDidLoad执行加载

-(void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self initSearchBar];
[self initWebView]; }

  三:实现seachBar的代理方法

#pragma UISearchBar

//点击searchbar上的GO  时调用
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
[self doSearch:searchBar];
} //点击键盘上的search时调用
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
[self doSearch:searchBar];
} //开始执行搜索
- (void)doSearch:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",searchBar.text]];
NSURLRequest *request =[NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}

在这里

  NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",searchBar.text]];
NSURLRequest *request =[NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];

这段代码就是为webView加载网页的方式,其他方式的还有

 //加载本地文件资源
// NSURL *url = [NSURL fileURLWithPath:@"文件路径"];
// NSURLRequest *request = [NSURLRequest requestWithURL:url];
// [webView loadRequest:request];
//读入一个HTML代码
// NSString *htmlPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"HTML文件地址"];
// NSString *htmlString = [NSString stringWithContentsOfFile: htmlPath encoding:NSUTF8StringEncoding error:NULL];
// [webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:htmlPath]];
四:实现webView加载失败时的代理方法
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"访问出错" message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alterview show];
}

另外,UIWebView常用的代理方法还有

- (void )webViewDidStartLoad:(UIWebView  *)webView
//网页开始加载的时候调用 - (void )webViewDidFinishLoad:(UIWebView *)webView
//网页加载完成的时候调用 -(BOOL )webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType )navigationType
//当UIWebView加载网页的时候就会调用到此函数,然后执行webViewDidStartLoad函数,可以在函数中进行请求解析,地址分析等

代码敲完后,来看一下运行的结果

ios开发入门篇(四):UIWebView结合UISearchBar的简单用法的更多相关文章

  1. iOS开发UI篇—多控制器和导航控制器简单介绍

    iOS开发UI篇—多控制器和导航控制器简单介绍 一.多控制器 一个iOS的app很少只由一个控制器组成,除非这个app极其简单.当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个vi ...

  2. ios开发入门篇(一):创建工程

    突然心血来潮,想写点技术方面的东西,做了ios也有好几年了,就简单的写个ios开发的技术博客,希望有人能用得到. 今天就先从创建一个Hellow World工程开始 一:首先打开xcode然后单击Cr ...

  3. ios开发入门篇(二):Objective-C的简单语法介绍

    一:面向对象的思想 objective-c与C语言的编程思想不同,C语言是面向过程的编程,而objective-c则是面向对象的编程,所谓面向对象,我个人的理解,就是抽象.将具有一定共同点的实物抽象成 ...

  4. ios开发入门篇(三):UITableView简介

    最近做项目又开始用到了uitableview,温习之余,在这里把uitableview的用法分享一下,有不对的地方欢迎大家提出来. 废话不多说,先创建一个工程,由于Xcode6,去除了创建工程时的空项 ...

  5. iOS开发UI篇—核心动画简介

    转自:http://www.cnblogs.com/wendingding/p/3801036.html iOS开发UI篇—核心动画简介 一.简单介绍 Core Animation,中文翻译为核心动画 ...

  6. iOS开发数据库篇—SQLite的应用

    iOS开发数据库篇—SQLite的应用 一.简单说明 在iOS中使用SQLite3,首先要添加库文件libsqlite3.dylib和导入主头文件. 导入头文件,可以使用库中的函数(是纯C语言的) 二 ...

  7. iOS开发拓展篇—音乐的播放

    iOS开发拓展篇—音乐的播放 一.简单说明 音乐播放用到一个叫做AVAudioPlayer的类,这个类可以用于播放手机本地的音乐文件. 注意: (1)该类(AVAudioPlayer)只能用于播放本地 ...

  8. iOS开发网络篇—XML数据的解析

     iOS开发网络篇—XML数据的解析 iOS开发网络篇—XML介绍 一.XML简单介绍 XML:全称是Extensible Markup Language,译作“可扩展标记语言” 跟JSON一样,也是 ...

  9. iOS开发多线程篇—线程的状态

    iOS开发多线程篇—线程的状态 一.简单介绍 线程的创建: self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(te ...

随机推荐

  1. Hadoop 2.0+YARN启动脚本分析与实战经验

    start-all.sh脚本现在已经废弃,推荐使用start-dfs.sh和start-yarn.sh分别启动HDFS和YARN. 在新一代的Hadoop里面HDFS称为了统一存储的平台,而YARN成 ...

  2. 赵雅智_ContentProvider

    ContentProvider介绍 ContentProvider是不同应用程序之间进行交换数据的标志API 也就是说:一个应用程序通过ContentProvider暴露自己的数据操作接口,那么无论该 ...

  3. Eclipse下如何导入jar包【转载】

    我们在用Eclipse开发程序的时候,经常想要用到第三方的jar包.这时候我们就需要在相应的工程下面导入这个jar包.以下配图说明导入jar包的步骤. 1.右击工程的根目录,点击Properties进 ...

  4. java 覆盖hashCode()深入探讨 代码演示样例

    java 翻盖hashCode()深入探讨 代码演示样例 package org.rui.collection2.hashcode; /** * 覆盖hashcode * 设计HashCode时最重要 ...

  5. [WebGL] Setting Up WebGL

    In this lesson we cover setting up WebGL for use, including creating a canvas, getting the WebGL ren ...

  6. iOS开发——网络编程Swift篇&(四)异步Get方式

    异步Get方式 // MARK: - 异步Get方式 func asynchronousGet() { //创建NSURL对象 var url:NSURL! = NSURL(string: " ...

  7. MySQL join buffer使用

      原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://huanghualiang.blog.51cto.com/6782683/12 ...

  8. AngularJS - 定时器 倒计时例子

    <body> <div ng-app="myApp"> <div ng-controller="firstController"& ...

  9. 【完结】利用 Composer 完善自己的 PHP 框架(三)——Redis 缓存

    本教程示例代码见 https://github.com/johnlui/My-First-Framework-based-on-Composer 回顾 上两篇文章中我们完成了 View 视图加载类和 ...

  10. 美国L1签证和B1,E2签证的区别

    L1是跨国公司派驻工作人员到美国关联公司工作所需的签证.L1有两种: L1A是给管理人员的.L1B是给关键技术人员的.通过延期,L1A最长时间可达7年. L1B最长时间可达五年. 最初的L1申请,如果 ...