ios开发入门篇(四):UIWebView结合UISearchBar的简单用法
生活随笔
收集整理的這篇文章主要介紹了
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函數,可以在函數中進行請求解析,地址分析等代碼敲完后,來看一下運行的結果
?
轉載于:https://www.cnblogs.com/zyi1992/p/4106662.html
總結
以上是生活随笔為你收集整理的ios开发入门篇(四):UIWebView结合UISearchBar的简单用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php实现设计模式之 适配器模式
- 下一篇: BZOJ2986 Non-Squaref