IOS开发-UI学习-UIWebView,简单浏览器的制作
制作一个简单的浏览器,包含网址输入框,Search按钮,前进、回退按钮,UIWebView就这几个简单的控件。
UITextField:用来输入网址;
UIbuttom:实现前进,后退,搜索等功能;
UIWebView:实现网页展示。
准备工作:
右键Info.plist并Open As Source Code,打开之后添加以下代码段:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
以上代码段功能:有些网址为Http,要搞成Https,具体原理以后探索出来了再补充。
言归正传,实现浏览器功能的具体代码如下:
#import "ViewController.h"
@interface ViewController (){
// 定义全局变量,各个控件
UIWebView *mywebview;
UIButton *backbutton;
UIButton *goforbutton;
UITextField *urlField;
UIButton *searchButton;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 添加浏览器view,
mywebview = [[UIWebView alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height-)];
[self.view addSubview:mywebview];
// 网址输入框
urlField = [[UITextField alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width-, )];
urlField.borderStyle = UITextBorderStyleRoundedRect;
urlField.text= mywebview.request.URL.absoluteString;
[self.view addSubview:urlField];
// search button
searchButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-, , , )];
searchButton.backgroundColor = [UIColor redColor];
[searchButton setTitle:@"Search" forState:UIControlStateNormal];
[searchButton addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:searchButton];
// 返回按键
backbutton = [[UIButton alloc]initWithFrame:CGRectMake(, self.view.frame.size.height-, , )];
backbutton.backgroundColor = [UIColor redColor];
[backbutton setTitle:@"返回" forState:UIControlStateNormal];
[backbutton addTarget:self action:@selector(backFbution:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backbutton];
// 前进按钮
goforbutton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-, self.view.frame.size.height-, , )];
goforbutton.backgroundColor = [UIColor redColor];
[goforbutton setTitle:@"前进" forState:UIControlStateNormal];
[goforbutton addTarget:self action:@selector(goFobution:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:goforbutton];
}
//返回按钮绑定事件
-(void)backFbution:(id)sender{
[mywebview goBack];
}
//前进按钮绑定事件
-(void)goFobution:(id)sender{
[mywebview goForward];
}
//搜索按钮绑定事件
-(void)search:(id)sender{
[mywebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlField.text]]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
运行效果如下(请无视UI所用颜色。。。。。):

存在一个问题,就是我在百度里面随便点击一个网址链接之后,可以打开网址,但打开之后浏览器的网址输入框中的网址依然是之前那个,没有改变为当前网站地址。这个问题遗留下来。。。
IOS开发-UI学习-UIWebView,简单浏览器的制作的更多相关文章
- iOS开发UI篇—UITabBarController简单介绍
iOS开发UI篇—UITabBarController简单介绍 一.简单介绍 UITabBarController和UINavigationController类似,UITabBarControlle ...
- iOS开发UI篇—Modal简单介绍
iOS开发UI篇—Modal简单介绍 一.简单介绍 除了push之外,还有另外一种控制器的切换方式,那就是Modal 任何控制器都能通过Modal的形式展⽰出来 Modal的默认效果:新控制器从屏幕的 ...
- iOS开发UI篇—Kvc简单介绍
ios开发UI篇—Kvc简单介绍 一.KVC简单介绍 KVC key valued coding 键值编码 KVC通过键值间接编码 补充: 与KVC相对的时KVO,即key valued observ ...
- iOS开发UI篇—UIWindow简单介绍
iOS开发UI篇—UIWindow简单介绍 一.简单介绍 UIWindow是一种特殊的UIView,通常在一个app中只会有一个UIWindow iOS程序启动完毕后,创建的第一个视图控件就是UIWi ...
- iOS开发UI篇—Quartz2D简单介绍
iOS开发UI篇—Quartz2D简单介绍 一.什么是Quartz2D Quartz 2D是⼀个二维绘图引擎,同时支持iOS和Mac系统 Quartz 2D能完成的工作: 绘制图形 : 线条\三角形\ ...
- iOS开发UI篇—Quartz2D简单使用(一)
iOS开发UI篇—Quartz2D简单使用(一) 一.画直线 代码: // // YYlineview.m // 03-画直线 // // Created by apple on 14-6-9. // ...
- iOS开发UI篇—Quartz2D简单使用(二)
iOS开发UI篇—Quartz2D简单使用(二) 一.画文字 代码: // // YYtextview.m // 04-写文字 // // Created by 孔医己 on 14-6-10. // ...
- iOS开发UI篇—Quartz2D简单使用(三)
iOS开发UI篇—Quartz2D简单使用(三) 一.通过slider控制圆的缩放 1.实现过程 新建一个项目,新建一个继承自UIview的类,并和storyboard中自定义的view进行关联. 界 ...
- iOS开发UI篇—popoverController简单介绍
iOS开发UI篇—popoverController简单介绍 一.简单介绍 1.什么是UIPopoverController 是iPad开发中常见的一种控制器(在iPhone上不允许使用) 跟其他控制 ...
- iOS开发UI篇—Quartz2D简单使用(一)
iOS开发UI篇—Quartz2D简单使用(一) 一.画直线 代码: 1 // 2 // YYlineview.m 3 // 03-画直线 4 // 5 // Created by apple on ...
随机推荐
- jsonp的简单实现
jsonp: function(url, data, callback){ if( wfQuery.isFunction(data) ){ callback = data; data = {}; } ...
- Codeforces Round #361 (Div. 2) C.NP-Hard Problem
题目连接:http://codeforces.com/contest/688/problem/C 题意:给你一些边,问你能否构成一个二分图 题解:二分图:二分图又称作二部图,是图论中的一种特殊模型. ...
- 为什么有时候必须添加sys.setdefaultencoding('utf-8')
今天在尝试Python的CGI模块时遇到中文字符不能正确显示的问题,很郁闷.在网上仔细找了找,终于解决了这个问题,现在将解决方法陈述如下,以防下次失误. 页面源代码如下 #-*- coding: ut ...
- margin:0 auto在ie7浏览器里面无效
把文件头改成 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w ...
- 链表基础 HDU1267
基础的链表,模拟一下就好了...就签个到
- hibernate 使用sql 查询(setResultTransformer)
使用方法举例如下: public List findByOid(Object oid) { log.debug("finding all WatershedAnalyse instance ...
- 货物搬运(move)
货物搬运(move) 题目描述 天地无情人有情,一方有难八方支援!汶川大地震发生后,灾区最紧缺的是救灾帐篷,全国各地支援的帐篷正紧急向灾区运送.假设围绕纹川县有环行排列的n个救灾帐篷的存储点,每个存储 ...
- ntopng汉化记录
对应版本为 ntopng-1.2.0_r8116.tgz 1.
- char*赋值在常量区,不可以修改
char*赋值在常量区,不可以修改,要想修改,用数组. char* = "abc";*(pCh+1) = 'k';//编译正常,运行报错. char pCh[] = "a ...
- HDU 2188 悼念512汶川大地震遇难同胞——选拔志愿者(基础巴什博奕)
最最最基础巴什博奕 #include<stdio.h> #include<iostream> #include<cstring> #include<cmath ...