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 ...
随机推荐
- Linux学习 -- Shell编程 -- 字符处理命令
sort排序命令 sort [选项] 文件名 -f 忽略大小m写 -n 按数值型,默认字符串型 -r 反向 -t 指定分隔符 -k n[,m] 指定字段范围,默认行尾 eg. sort -n -t & ...
- LightOJ 1234 Harmonic Number 调和级数部分和
题目链接:http://lightoj.com/volume_showproblem.php?problem=1234 Sample Input Sample Output Case : Case : ...
- php 文件夹遍历俩种对比
configu.phpindex.php新建文件夹 D:\xampp\htdocs\1test\use\useversion/configu.phpD:\xampp\htdocs\1test\use\ ...
- Chapter 1 First Sight——24
He looked away quickly, more quickly than I could, though in a flush of embarrassment I dropped my e ...
- $.ajax和$.post的区别(前者根据key-value/后者根据形参)
post不需要给key-value形式: $("#btn").click(function(){ var url=basePath+"/emp/login"; ...
- 转 Fragment 和 FragmentActivity的使用
今天学习下 Android中的 Fragment 和 FragmentActivity,因为没有4.0手机,平台是2.3.3 所以我是使用 v4 support 包来进行学习. 要想用Fragment ...
- 【转】Build Your own Simplified AngularJS in 200 Lines of JavaScript
原文:http://blog.mgechev.com/2015/03/09/build-learn-your-own-light-lightweight-angularjs/ Build Your o ...
- javascript event bubbling and capturing (再谈一谈js的事件冒泡和事件补获,看到这篇文章加深了理解)
原文地址:http://javascript.info/tutorial/bubbling-and-capturing 先给出最终的结论: Summary Events first are captu ...
- FACE++学习一、detect接口
/detection/detect 描述 检测给定图片(Image)中的所有人脸(Face)的位置和相应的面部属性 目前面部属性包括性别(gender), 年龄(age), 种族(race), 微笑程 ...
- '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "AttentionController" nib but the view outlet was not set.'
问题出现原因: 先创建控制器的.h与.m文件,后来创建了相应的xib文件,运行后出现这个问题. 解决办法: