日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

ios开发入门篇(四):UIWebView结合UISearchBar的简单用法

發(fā)布時間:2025/4/16 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios开发入门篇(四):UIWebView结合UISearchBar的简单用法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

  ?UIWebView是ios開發(fā)中比較常用的一個控件。我們可以用它來瀏覽網(wǎng)頁、打開文檔等,今天筆者在這里簡單介紹下UIWebView和UISearchBar結(jié)合起來的用法,做一個簡單的類瀏覽器。

  一:首先定義這兩個控件,并在.h文件中實現(xiàn)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上按鈕的默認(rèn)文字為Cancel,這里改為“GO”版本不同方法有些許區(qū)別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]; //設(shè)置是否支持交互[self.webView setDelegate:self]; //接受委托[self.webView setScalesPageToFit:YES]; //設(shè)置自動縮放 [self.view addSubview:self.webView]; }

在viewDidLoad執(zhí)行加載

-(void)viewDidLoad {[super viewDidLoad];[self.view setBackgroundColor:[UIColor whiteColor]];[self initSearchBar];[self initWebView];}

?  三:實現(xiàn)seachBar的代理方法

#pragma UISearchBar//點擊searchbar上的GO 時調(diào)用 - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{[self doSearch:searchBar]; }//點擊鍵盤上的search時調(diào)用 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{[searchBar resignFirstResponder];[self doSearch:searchBar]; }//開始執(zhí)行搜索 - (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加載網(wǎng)頁的方式,其他方式的還有

//加載本地文件資源 // 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]]; 四:實現(xiàn)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 //網(wǎng)頁開始加載的時候調(diào)用- (void )webViewDidFinishLoad:(UIWebView *)webView //網(wǎng)頁加載完成的時候調(diào)用-(BOOL )webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType )navigationType //當(dāng)UIWebView加載網(wǎng)頁的時候就會調(diào)用到此函數(shù),然后執(zhí)行webViewDidStartLoad函數(shù),可以在函數(shù)中進(jìn)行請求解析,地址分析等

代碼敲完后,來看一下運行的結(jié)果

?

轉(zhuǎn)載于:https://www.cnblogs.com/zyi1992/p/4106662.html

總結(jié)

以上是生活随笔為你收集整理的ios开发入门篇(四):UIWebView结合UISearchBar的简单用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。