ios開發入門篇(四):UIWebView結合UISearchBar的簡單用法

來源:互聯網
上載者:User

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(0, 20, [UIScreen mainScreen].bounds.size.width, 40)];    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(0, 60, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-60)];    [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函數,可以在函數中進行請求解析,地址分析等

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.