Objective-c 网络编程3 UIWebView 搜索框

//
//  ViewController.m
//  UIWebView
//
//  Created by DC017 on 15/12/10.
//  Copyright © 2015年 DC017. All rights reserved.
//
#pragma mark 有搜索框
#import "ViewController.h"

@interface ViewController ()<UISearchBarDelegate,UIWebViewDelegate>
{
    
    //搜索栏
    UISearchBar * searchbar;
    UIWebView * webview;
    UIToolbar * toolbar;
    UIBarButtonItem * qianjin;
    UIBarButtonItem * houtui;
    
    
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layout];
   
}

-(void)layout{
    
    searchbar =[[UISearchBar alloc]initWithFrame:CGRectMake(0, 20, 375, 44)];
    [self.view addSubview:searchbar];
     webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 64, 375,559)];
     webview.backgroundColor=[UIColor grayColor];
    [self.view addSubview:webview];
     toolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 630, 375,44 )];
    
    //toolbar.items=@[按钮1,空格,按钮2];
    qianjin=[[UIBarButtonItem alloc]initWithTitle:@"前进" style:UIBarButtonItemStyleDone target:self action:@selector(qianjinI)];
    
    
    
    
   
    houtui=[[UIBarButtonItem alloc]initWithTitle:@"后退" style:UIBarButtonItemStyleDone target:self action:@selector(houtuiI)];
    
    //创建弹簧
    UIBarButtonItem *spring = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    toolbar.items=@[houtui,spring,qianjin];
    
    [self.view addSubview:toolbar];
    
    //遵守协议
    searchbar.delegate=self;
    webview.delegate=self;
    
    
 
   
}
#pragma mark searchBar 代理

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self request: searchbar.text];
}
-(void)request:(NSString *)searchbartext{
    NSString *strUrl=searchbartext;
    NSURL *url;;
    if ([strUrl hasPrefix:@"file://"]) {
        NSLog(@"sjdfj");
        NSRange range=[strUrl rangeOfString:@"file://"];
        NSString * fileName=[strUrl substringFromIndex:range.length];
        url=[[NSBundle mainBundle]URLForResource:fileName withExtension:nil];
    }else if([strUrl hasPrefix:@"http://"]){
       
        url=[NSURL URLWithString:strUrl];
    }else{
        NSString * strUrlI=[NSString stringWithFormat:@"http:www.baidu.com/s?wd=%@",strUrl];
        strUrl=[strUrlI stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
        
        url=[NSURL URLWithString:strUrlI];
    }
    
    NSURLRequest * requestI=[NSURLRequest requestWithURL:url];
    
    [webview loadRequest:requestI];
    
    NSLog(@"%@",strUrl);
    //NSURLRequest
    
}
-(void)qianjinI{
    [webview goForward];
    
}
-(void)houtuiI{
    [webview goBack];
    
}
#pragma mark webview 代理协议

#pragma mark webview 加载完成
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
     NSLog(@"rrrrrrr %@",webview.request.URL);
    

    searchbar.text=[NSString stringWithFormat:@"%@",webview.request.URL];
}
#pragma mark webview 开始加载
-(void)webViewDidStartLoad:(UIWebView *)webView{
   
    [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


转载于:https://my.oschina.net/u/2483781/blog/542213

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值