国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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

2019-11-14 19:51:33
字體:
來源:轉載
供稿:網友

   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函數,可以在函數中進行請求解析,地址分析等

代碼敲完后,來看一下運行的結果

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永昌县| 汉源县| 宣武区| 墨玉县| 环江| 罗源县| 永济市| 镇坪县| 浏阳市| 灵石县| 泗水县| 中江县| 兴海县| 昭苏县| 乐东| 建水县| 寻乌县| 定远县| 田阳县| 巴林左旗| 台安县| 聂荣县| 阳山县| 报价| 六安市| 浦北县| 巴青县| 德庆县| 北海市| 北流市| 永春县| 资溪县| 沧源| 吴川市| 盐城市| 玛沁县| 永宁县| 五寨县| 伊金霍洛旗| 平南县| 衢州市|