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函數,可以在函數中進行請求解析,地址分析等
代碼敲完后,來看一下運行的結果
新聞熱點
疑難解答