UIWebView is one of the more common controls in iOS development. We can use it to browse the Web, open the document, and so on, today I briefly introduce the following UIWebView and uisearchbar combination of usage, do a simple class browser.
One: Define these two controls first, and implement Uisearchbardelegate in. h files, uiwebviewdelegate two agents
@interface testview:uiviewcontroller<uisearchbardelegate, uiwebviewdelegate>
@property (nonatomic) uisearchbar* searchbar;
@property (nonatomic,retain) UIWebView* WebView;
Two: Load the two controls
//Load Searcbar-(void) initsearchbar{Self.searchbar= [[Uisearchbar alloc]initwithframe:cgrectmake (0, -, [UIScreen Mainscreen].bounds.size.width, +)]; Self.searchbar.Delegate=Self ; Accept a delegateSelf.searchBar.text=@"/ http"; //The default text for the button on the Uisearchbar is cancel, which is a bit different from the "GO" version for(IDCcinch[Self.searchbar Subviews]) { for(UIView *viewinch[cc Subviews]) { if([Nsstringfromclass (view.class) Isequaltostring:@"Uinavigationbutton"]) {UIButton*BTN = (UIButton *) view; [BTN Settitle:@"GO"Forstate:uicontrolstatenormal]; }}} [Self.view AddSubview:self.searchBar]; }//Loading WebView-(void) initwebview{Self.webview= [[UIWebView alloc] Initwithframe:cgrectmake (0, -, [UIScreen mainscreen].bounds.size.width, [UIScreen mainscreen].bounds.size.height- -)]; [Self.webview Setuserinteractionenabled:yes]; //Settingswhether to support interaction[Self.webview setdelegate:self];//AcceptDelegate[Self.webview Setscalespagetofit:yes];//SettingsAuto Scale[Self.view AddSubview:self.webView];}
performing the load on Viewdidload
-(void) viewdidload{ [Super Viewdidload]; [Self.view Setbackgroundcolor:[uicolor Whitecolor]; [Self initsearchbar]; [Self Initwebview]; }
Three: Implement the Agent method of Seachbar
#pragmaUisearchbar//call when you click Go on Searchbar- (void) searchbarcancelbuttonclicked: (Uisearchbar *) searchbar{[self Dosearch:searchbar];}//call when you click Search on the keyboard- (void) searchbarsearchbuttonclicked: (Uisearchbar *) searchbar{[Searchbar Resignfirstresponder]; [Self Dosearch:searchbar];}//Start a search- (void) Dosearch: (Uisearchbar *) searchbar{[Searchbar Resignfirstresponder]; Nsurl* URL = [Nsurl urlwithstring:[nsstring stringWithFormat:@"%@", Searchbar.text]]; Nsurlrequest*request =[Nsurlrequest Requestwithurl:url]; [Self.webview loadrequest:request];}
Over here
nsurl* url = [Nsurl urlwithstring:[nsstring stringwithformat:@ "%@", Searchbar.text]]; *request =[Nsurlrequest requestwithurl:url]; [Self.webview loadrequest:request];
This code is the way to load the Web page for WebView, and in other ways
//load local File resource//Nsurl *url = [nsurl fileurlwithpath:@ "file path"];//nsurlrequest *request = [Nsurlrequest requestwithurl:url];//[WebView loadrequest:request];//read in 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];
Four: Proxy method to implement WebView load failure
-(void) WebView: (UIWebView *) WebView didfailloadwitherror: (Nserror *) error{ *alterview = [[ Uialertview alloc] Initwithtitle:@ " access error " message:[error localizeddescription] Delegate: Nil cancelbuttontitle:nil otherbuttontitles:@ "OK", nil]; [Alterview show];}
In addition, the UIWebView commonly used proxy method also has
-(void ) Webviewdidstartload: (UIWebView *) webView // when the Web page starts to load, call -( void ) Webviewdidfinishload: (UIWebView *) webView // page load completed when called -(BOOL) WebView: ( UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) Navigationtype// when UIWebView loads the Web page, it is called to this function, Then execute the Webviewdidstartload function, can be in the function of request parsing, address analysis, etc.
After the code is knocked out, look at the results of the run
iOS Development Primer (iv): Simple usage of UIWebView combined with Uisearchbar