Ios development entry (4): simple usage of UIWebView combined with UISearchBar

Source: Internet
Author: User

Ios development entry (4): simple usage of UIWebView combined with UISearchBar
UIWebView is a commonly used control in ios development. We can use it to browse Web pages and open documents. Today, I will briefly introduce how to combine UIWebView and UISearchBar to create a simple browser. 1: first define the two controls and. implement callback in the hfile. Two proxies, UIWebViewDelegate, @ interface TestView: UIViewController <response, UIWebViewDelegate> @ property (nonatomic) UISearchBar * searchBar; @ property (nonatomic, retain) UIWebView * webView; 2. Load the two controls and copy the code. // load searcBar-(void) initSearchBar {self. searchBar = [[UISearchBar alloc] initWithFrame: CGRectMake (0, 20, [UIScreen mainScreen]. bounds. size. width, 40)]; self. searchBar. delegate = self; // accept the delegate self. searchBar. text = @ "http: //"; // The default text of the button on UISearchBar is Cancel. Here it is changed to "GO". There are some differences between different methods for (id cc in [self. searchBar subviews]) {for (UIView * view in [cc subviews]) {if ([NSStringFromClass (view. class) ispolictostring: @ "UINavigationButton"]) {UIButton * btn = (UIButton *) view; [btn setTitle: @ "GO" forState: UIControlStateNormal] ;}} [self. view addSubview: self. searchBar];} // load 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]; // sets whether interaction is supported [self. webView setDelegate: self]; // accept the delegate [self. webView setScalesPageToFit: YES]; // sets automatic scaling [self. view addSubview: self. webView];} copy the code by loading and copying the code in viewDidLoad-(void) viewDidLoad {[super viewDidLoad]; [self. view setBackgroundColor: [UIColor whiteColor]; [self initSearchBar]; [self initWebView];} copy code 3: copy Code # pragma UISearchBar // call-(void) searchBarCancelButtonClicked (UISearchBar *) searchbar {[self doSearch: searchBar] When you click GO On the searchBar.} // call-(void) searchBarSearchButtonClicked when you click search on the keyboard: (UISearchBar *) searchBar {[searchBar resignFirstResponder]; [self doSearch: searchBar];} // start to execute the search-(void) doSearch :( UISearchBar *) searchBar {[searchBar resignFirstResponder]; NSURL * url = [NSURL URLWithString: [NSString stringWithFormat: @ "% @", searchBar. text]; NSURLRequest * request = [NSURLRequest requestWithURL: url]; [self. webView loadRequest: request];} copy the code here NSURL * url = [NSURL URLWithString: [NSString stringWithFormat: @ "% @", searchBar. text]; NSURLRequest * request = [NSURLRequest requestWithURL: url]; [self. webView loadRequest: request]; this code is used to load web pages for webView. Other methods include copying code // loading local file resources // NSURL * url = [NSURL fileURLWithPath: @ "file path"]; // NSURLRequest * request = [NSURLRequest requestWithURL: url]; // [webView loadRequest: request]; // read an HTML code // NSString * htmlPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent: @ "HTML file address"]; // NSString * htmlString = [NSString stringWithContentsOfFile: htmlPath encoding: NSUTF8StringEncoding error: NULL]; // [webView loadHTMLString: htmlString baseURL: [NSURL fileURLWithPath: htmlPath]; copy code 4: proxy Method for webView loading failure-(void) webView :( UIWebView *) webView didFailLoadWithError :( NSError *) error {UIAlertView * alterview = [[UIAlertView alloc] initWithTitle: @ "Access error" message: [error localizedDescription] delegate: nil cancelButtonTitle: nil otherButtonTitles: @ "OK", nil]; [alterview show];} In addition, common proxy methods of UIWebView include copying code-(void) webViewDidStartLoad :( UIWebView *) webView // call-(void) webViewDidFinishLoad :( UIWebView *) when the web page starts loading *) webView // call-(BOOL) webView :( UIWebView *) webView shouldStartLoadWithRequest (NSURLRequest *) request navigationType :( UIWebViewNavigationType) when the webpage is loaded) navigationType // This function is called when the UIWebView loads the webpage, and then the webViewDidStartLoad function is executed to perform Request Parsing and address analysis in the function.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.