iOS 常见一些原件

 
   UIToolbar;//工具条
    UIBarButtonItem;//共具条的控件

  //创建一个导航栏

    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

    //创建一个导航栏集合

    UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:nil];

    //在这个集合Item中添加标题,按钮

    //style:设置按钮的风格,一共有三种选择

    //action:@selector:设置按钮的点击事件

    //创建一个左边按钮

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"左边" style:UIBarButtonItemStyleDone target:self action:nil];

    //创建一个右边按钮

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右边" style:UIBarButtonItemStyleDone target:self action:@selector(clickRightButton)];

       //设置导航栏的内容

    [navItem setTitle:@"凌凌漆"];

       //把导航栏集合添加到导航栏中,设置动画关闭

    [navBar pushNavigationItem:navItem animated:NO];

    

    //把左右两个按钮添加到导航栏集合中去

    [navItem setLeftBarButtonItem:leftButton];

    [navItem setRightBarButtonItem:rightButton];

    

    //将标题栏中的内容全部添加到主视图当中

    [self.view addSubview:navBar];


    UISearchBar;//搜索条
    UITextView;//文本段落输入框
    UIAlertView;//警告框
 * UIAlertViewStyleDefault          基本样式的警告框
     * UIAlertViewStyleSecureTextInput  带一个密文输入框的警告框
     * UIAlertViewStylePlainTextInput   带一个明文输入框的警告框
     * UIAlertViewStyleLoginAndPasswordInput带有明文+密文样式输入框的警告框
    UIActionSheet;//提示框
    UIPickerView;//选择器
设置选中的picker中的某一行
//UI
    UIDatePicker;//时间选择器
    
    UIActivity;//进度轮
    UIWebView;//网页
   
 
 

刚接触IOS开发1年多,现在对于 混合式 移动端开发越来越流行,因为开发成本上、速度上都比传统的APP开发要好,混合式开发 是传统模式与PC网页端相结合的模式。那么提到了 APP的混合模式开发,在Android开发中有WebView作为混合模式开发的桥梁,当然在IOS中也同样有一个 UIWebView 组件来作为混合模式开发的桥梁,那么下面就对UIWebView的一些基本知识详解一下。

一、UIWebView的基础使用

1、创建UIWebView:

CGRect bouds = [[UIScreen manScreen]applicationFrame];
UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];

2、设置属性:

webView.scalespageToFit = YES;//自动对页面进行缩放以适应屏幕
webView.detectsPhoneNumbers = YES;//自动检测网页上的电话号码,单击可以拨打

3、显示网页视图UIWebView:

[self.view addSubview:webView];

4、加载内容

NSURL* url = [NSURL URLWithString:@"http://www.baidu.com"];//创建URL
NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest
[webView loadRequest:request];//加载

也可以加载一个本地资源:

NSURL* url = [NSURL fileURLWithPath:filePath];//创建URL
NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest
[webView loadRequest:request];//加载

UIWebView 还支持将一个NSString对象作为源来加载。你可以为其提供一个基础URL,来指导UIWebView对象如何跟随链接和加载远程资源:

[webView loadHTMLString:myHTML baseURL:[NSURL URLWithString:@"http://baidu.com"]];

5、导航

UIWebView类内部会管理浏览器的导航动作,通过goForward和goBack方法你可以控制前进与后退动作:

[webView goBack];
[webView goForward];
[webView reload];//重载
[webView stopLoading];//取消载入内容

6、UIWebViewDelegate委托代理

UIWebView支持一组委托方法,这些方法将在特定时间得到通知。要使用这些方法,必须先设定webView的委托:

webView.delegate = self;

下面每个委托方法的第一个参数都是指向一个UIwebview的指针,因此你可以将一个委托用于多个网页视图。

-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*) reuqest navigationType: (UIWebViewNavigationType)navigationType;//当网页视图被指示载入内容而得到通知。应当返回YES,这样会进行加载。通过导航类型参数可以得到请求发起的原因,可以是以下任意值:
UIWebViewNavigationTypeLinkClicked
UIWebViewNavigationTypeFormSubmitted
UIWebViewNavigationTypeBackForward
UIWebViewNavigationTypeReload
UIWebViewNavigationTypeFormResubmitted
UIWebViewNavigationTypeOther

UIWebView控件加载网页的监听函数方法:

-(void)webViewDidStartLoad:(UIWebView*)webView ;//当网页视图已经开始加载一个请求后,得到通知。
-(void)webViewDidFinishLoad:(UIWebView*)webView ;//当网页视图结束加载一个请求之后,得到通知。
-(void)webView:(UIWebView*)webView DidFailLoadWithError:(NSError*)error;//当在请求加载中发生错误时,得到通知。会提供一个NSSError对象,以标识所发生错误类型。

以上是IOS中UIWebView的基础使用要点详解,接下来一些UIWebView的常用注意点。

 

二、IOS中UIWebView常用注意点:

1、与UIWebView进行交互,调用web页面中的需要传参的函数时,参数需要带单引号,或者双引号(双引号需要进行转义在转义字符前加\),在传递json字符串时不需要加单引号或双引号:

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *sendJsStr=[NSString stringWithFormat:@"openFile(\"%@\")",jsDocPathStr];
[webView stringByEvaluatingJavaScriptFromString:sendJsStr];
}

2、在该代理方法中判断与webView的交互,可通过html里定义的协议实现:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType

3、只有在webView加载完毕之后在能够调用对应页面中的js方法。(对应方法如第1条).

4、为webView添加背景图片:

approvalWebView.backgroundColor=[UIColor clearColor];
approvalWebView.opaque=NO;//这句话很重要,webView是否是不透明的,no为透明 在webView下添加个imageView展示图片就可以了

5、获取webView页面内容信息:

NSString *docStr=[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];//获取web页面内容信息,此处获取的是个json字符串
SBJsonParser *parserJson=[[[SBJsonParser alloc]init]autorelease];
NSDictionary *contentDic=[parserJson objectWithString:docStr];//将json字符串转化为字典

6、 加载本地文件的方法:

//第一种方法:
NSString* path = [[NSBundle mainBundle] pathForResource:name ofType:@"html" inDirectory:@"mobile"];//mobile是根目录,name是文件名称,html是文件类型
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]]; //加载本地文件
//第二种方法:
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *filePath = [resourcePath stringByAppendingPathComponent:@"mobile.html"];
NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[uiwebview loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

7、将文件下载到本地址然后再用webView打开:

NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
self.filePath = [resourceDocPath stringByAppendingPathComponent:[NSString stringWithFormat:@"maydoc%@",docType]];
NSData *attachmentData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:theUrl]];
[attachmentData writeToFile:filePath atomically:YES];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[attachmentWebView loadRequest:requestObj];
//删除指定目录下的文件
NSFileManager *magngerDoc=[NSFileManager defaultManager];
[magngerDoc removeItemAtPath:filePath error:nil];

8、处理webView展示txt文档乱码问题:

if ([theType isEqualToString:@".txt"])
{
//txt分带编码和不带编码两种,带编码的如UTF-8格式txt,不带编码的如ANSI格式txt
//不带的,可以依次尝试GBK和GB18030编码
NSString* aStr = [[NSString alloc] initWithData:attachmentData encoding:NSUTF8StringEncoding];
if (!aStr)
{
//用GBK进行编码
aStr=[[NSString alloc] initWithData:attachmentData encoding:0x80000632];
}
if (!aStr)
{
//用GBK编码不行,再用GB18030编码
aStr=[[NSString alloc] initWithData:attachmentData encoding:0x80000631];
}

//webView 高度获取

- (void)webViewDidFinishLoad:(UIWebView *)webView <br>

{ CGRect frame = webView.frame; <br>
NSString *fitHeight = [webview stringByEvaluatingJavaScriptFromString:@ "document.body.scrollHeight;" ]; <br>
frame.size.height = [fitHeight floatValue]; <br>
webView.frame = frame; <br>
}<br>

 
 
 

    UIScreenMode;//滚动视图
    UIPageControl;//小白点
    UITableView;//表视图
    UICollectionView;//网格状的表视图
  

转载于:https://www.cnblogs.com/ningguozhu/p/4992653.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值