ios开发入门篇(四):UIWebView结合UISearchBar的简单用法
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的简单用法的更多相关文章
- iOS开发UI篇—多控制器和导航控制器简单介绍
iOS开发UI篇—多控制器和导航控制器简单介绍 一.多控制器 一个iOS的app很少只由一个控制器组成,除非这个app极其简单.当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个vi ...
- ios开发入门篇(一):创建工程
突然心血来潮,想写点技术方面的东西,做了ios也有好几年了,就简单的写个ios开发的技术博客,希望有人能用得到. 今天就先从创建一个Hellow World工程开始 一:首先打开xcode然后单击Cr ...
- ios开发入门篇(二):Objective-C的简单语法介绍
一:面向对象的思想 objective-c与C语言的编程思想不同,C语言是面向过程的编程,而objective-c则是面向对象的编程,所谓面向对象,我个人的理解,就是抽象.将具有一定共同点的实物抽象成 ...
- ios开发入门篇(三):UITableView简介
最近做项目又开始用到了uitableview,温习之余,在这里把uitableview的用法分享一下,有不对的地方欢迎大家提出来. 废话不多说,先创建一个工程,由于Xcode6,去除了创建工程时的空项 ...
- iOS开发UI篇—核心动画简介
转自:http://www.cnblogs.com/wendingding/p/3801036.html iOS开发UI篇—核心动画简介 一.简单介绍 Core Animation,中文翻译为核心动画 ...
- iOS开发数据库篇—SQLite的应用
iOS开发数据库篇—SQLite的应用 一.简单说明 在iOS中使用SQLite3,首先要添加库文件libsqlite3.dylib和导入主头文件. 导入头文件,可以使用库中的函数(是纯C语言的) 二 ...
- iOS开发拓展篇—音乐的播放
iOS开发拓展篇—音乐的播放 一.简单说明 音乐播放用到一个叫做AVAudioPlayer的类,这个类可以用于播放手机本地的音乐文件. 注意: (1)该类(AVAudioPlayer)只能用于播放本地 ...
- iOS开发网络篇—XML数据的解析
iOS开发网络篇—XML数据的解析 iOS开发网络篇—XML介绍 一.XML简单介绍 XML:全称是Extensible Markup Language,译作“可扩展标记语言” 跟JSON一样,也是 ...
- iOS开发多线程篇—线程的状态
iOS开发多线程篇—线程的状态 一.简单介绍 线程的创建: self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(te ...
随机推荐
- JQuery的Ajax使用Get,Post方法调用C#WebService并返回数据
本文将介绍jQuery调用基于.NET Framework 3.5的WebService返回JSON数据,另外还要介绍一下用jQuery调用WebService的参数设置及设置不当所出现的问题,还有出 ...
- git 与 github 最简单的用法
今天发现cygwin里已经装了git,所以顺便测试一下git是怎么用的. 大概最简单的用法是这样的: 1.在github创建一个repository.复制右边那个 HTTPS clone URL 2. ...
- 如何给你的VS2010添加创建文件后的头注释
修改VS自带的模板 1) 类文件 D:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplatesCache\ ...
- JAVA(2)——JDBC
刚接触JDBC的时候,有时候就在想,为什么java要用JDBC,而不是直接使用之前在VB中就学习过的ODBC,由于ODBC是在JDBC之前出现,所以ODBC肯定由于无法完毕某些操作或者不能非常顺利的完 ...
- replace和insert的语句插入(转)
SELECT INTO 和 INSERT INTO SELECT 两种表复制语句 Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) valu ...
- Advice on improving your programming skills
Programming is cool. But behind the scenes it's also difficult for many people. Many people are defe ...
- launch failed.Binary not found in Linux/Ubuntu解决方案
Linux下出现launch failed.Binary not found的解决方案: 首先当你把网上关于mingw的解决方案都看晕了的时候,告诉你,别看关于mingw的了.Linux下不用ming ...
- 《Entity Framework 6 Recipes》中文翻译——第十章EntityFramework存储过程处理(八)
将插入.更新和删除操作映射到存储过程 问题 您想在存储过程中映射插入.更新和删除操作. 解决方案 下图所示的运动员Athlete实体模型.底层数据库有一张运动员Athlete表.您想使用存储过程进行实 ...
- Creating a Mono 3 RPM on CentOS
Creating a Mono 3 RPM on CentOS A quick guide to creating an rpm of mono 3 from source, starting wit ...
- c# 友元程序集
在团队开发中,如果一个程序集中要调用另外一个程序集,但是要被调用的那个程序集又不想用public来公开自己的类, 那么怎么办,就是用最后一种internal来用来做类的可见性了. 下面来看一个简单例子 ...