Reading notes-uiview and controls

Source: Internet
Author: User
Tags uicontrol file transfer protocol

1, UIView

In Objective-c, NSObject is the "root" class for all classes. Similarly, in the Uikit framework, there is an amazing class UIView. From an inheritance relationship, UIView is the root of all views.

1.1. UIView Family

UIView is broadly divided into "control" and "view", both of which inherit from UIView.

The Uicontrol class is a control class that is called because they are capable of responding to some advanced events. Views other than the Uicontrol class do not have these advanced events.

1.2. The construction level of the application interface

(left) is a build-level diagram of an application interface that has a uiwindow that contains a uiview root view. The root view also has 3 sub-views-button1, Label2, and UIView (VIEW2), where a button uiview exists in the child View View2 (Button3).

In general, only one UIWindow is included in the application. From a view-building level, UIWindow contains a root view UIView. The root view is usually only one, which is placed in the UIWindow. The type of the root view determines the type of application.

The construction level of the application interface is a tree structure, UIWindow is the "root", the root view is "trunk", the other object is the canopy. In a hierarchy, the top and bottom two views are parent-child relationships. In addition to UIWindow, each view has a parent view and only one, and the child view can have multiple.

Superview, gets the parent view object. Subviews, gets a collection of child view objects. Window to get the UIWindow object where the view resides.

1.3. View classification

For ease of development, Apple will uikit a few categories of views in the frame.

Control. Inherits from the Uicontrol class and is capable of responding to user advanced events.

Window. It is a UIWindow object. An iOS app has only one UIWindow object, which is the "root" container for all child views.

The container view. It includes Uiscrollview, Uitoolbar, and their subclasses. Uitoolbar is a very special container that can contain other controls, which are usually placed at the bottom of the screen and can be placed at the top of the screen in special cases.

Displays the view. Used to display information, including Uiimageview, UILabel, Uiprogressview, and Uiactivityindicatorview.

Text and Web views. Provides views that can display multiple lines of text, including Uitextview and UIWebView, where Uitextview is also a container view, UIWebView the ability to load and display HTML code views.

Navigation view. Provides users with a navigation (or jump) view from one screen to another, including Uitabbar and Uinavigationbar.

Warning boxes and action tables. Used to provide feedback to users or to interact with users. The Uialertview view is a warning box that bounces out in an animated form, while the Uiactionsheet view gives the user an optional action that slides out of the bottom of the screen.

In fact, many views (such as Uilabel, text view and progress bar, etc.) did not inherit Uicontrol, but we are also accustomed to become "control", which is commonly known as the development of a common classification method, and the strict sense of the concept of classification is different.

2, TextField and TextView

2.1. In the Uikit framework, the TextField control is created by the Uitextfield class. In addition, it has a corresponding Uitextfielddelegate delegation protocol. Delegates can help respond to event handling. TextField inherits the Uicontrol, is subordinate to the real "control", and TextView inherits the Uiscrollview.

2.2. TextView is a control that can display and edit multiple lines of text, created by the Uitextview class. The TextView control has a corresponding Uitextviewdelegate delegate protocol, and we can respond to events with delegates.

2.3, once TextField and TextView and other controls in the editing state, the system will be smart pop-up keyboard, but the keyboard is not so smooth, we need to use code to achieve.

When TextField or TextView are in the editing state, these controls become "first responders." To turn off the keyboard, you discard the "first responder" identity. In iOS, an event travels along the responder chain from one responder to the next, and if one of the responders does not respond to the event, the event is passed down again.

3.WebView

3.1. The WebView control can load local HTML code or network resources.

3.1.1 Local resources are loaded synchronously, and the data can originate from local files or hard-coded HTML strings. Here's how:

Loadhtmlstring:baseurl. Sets the base path of the home page file and loads the page data through an HTML string.

LoadData:MIMEType:textEncodeingName:baseURL. Specifies the MIME type, the encoding set, and the NSData object to load a home page data and set the base path for the home file.

With these two methods, you need to be aware of character set issues, and what character sets to use depends on the HTML file.

3.1.2 Load network resources, we use asynchronous loading method, using Loadrequest: (Nsurlrequest *) request, the method requires a Nsurlrequest object, The object must be constructed in strict compliance with some protocol format, such as: Http://www.baidu.com,HTTP protocol, file://localhost/Users/abc.../index.html, file Transfer protocol. Where heep://and file://are protocol names and cannot be omitted.

Since we are loading webview with asynchronous requests, we also implement the corresponding Uiwebviewdelegate delegate protocol to respond to WebView events at different stages of loading by implementing the Protocol.

Let's take a look at the usage of the 3 methods of WebView in this case. The case has 3 buttons, loadhtmlstring, LoadData, and Loadrequest, and clicking on these three buttons triggers the WebView 3 loading methods respectively.

In the ViewController.h file, define the export and action, the code is as follows:

1 #import<UIKit/UIKit.h>2 3 @interfaceViewcontroller:uiviewcontroller <UIWebViewDelegate>4 5@property (Weak, nonatomic) iboutlet UIWebView *WebView;6 7-(Ibaction) Testloadhtmlstring: (ID) sender;8-(Ibaction) Testloaddata: (ID) sender;9-(Ibaction) Testloadrequest: (ID) sender;Ten  One @end

In the code above, we define 3 action methods and a UIWebView export property. In the viewcontroller.m file, testloadhtmlstring: and Testloaddata: The code for the method is as follows:

1-(Ibaction) Testloadhtmlstring: (ID) Sender {2 3NSString *htmlpath =[[NSBundle Mainbundle] Pathforresource:4             @"Index"OfType:@"HTML"];5Nsurl *bundleurl =[Nsurl Fileurlwithpath:[[nsbundle Mainbundle] bundlepath];6Nserror *error =Nil;7 8NSString *html =[[NSString alloc] Initwithcontentsoffile:9Htmlpath encoding:nsutf8stringencoding error:&ERROR];Ten  One         if(Error = = nil) {//Data Load???? There is no error in the situation???? Under A [Self.webview loadhtmlstring:html baseurl:bundleurl]; -         } - } the  --(Ibaction) Testloaddata: (ID) Sender { -  -NSString *htmlpath =[[NSBundle Mainbundle] +Pathforresource:@"Index"OfType:@"HTML"]; -Nsurl *bundleurl =[Nsurl Fileurlwithpath:[[nsbundle Mainbundle] bundlepath]; +Nserror *error =Nil; A  atNSData *htmldata =[[NSData alloc] initwithcontentsoffile:htmlpath]; -  -         if(Error = = nil) {//Data Load???? There is no error in the situation???? Under -[Self.webview loaddata:htmldata MIMEType:@"text/html"Textencodingname:@"UTF-8"Baseurl:bundleurl]; -         }  -}

These two methods are used to load the local resource file index.html and display it on WebView. In the first method, a

NSString *html = [[NSString alloc] Initwithcontentsoffile:htmlpath encoding:nsutf8stringencoding error:&error];

method to read the contents of the index.html file into the NSString object. In the read process, you need to use the encoding parameter to specify the character set as the Nsutf8stringencoding,error parameter to determine if the read is successful, and if the error = = Nil Indicates that the read succeeded and vice versa. In the [Self.webview loadhtmlstring:html Baseurl:bundleurl] statement, the BaseURL parameter is used to set the base path of the home file. The resource directory where the index.html is located, which can be nsurl fileurlwithpath:[[nsbundle Mainbundle] Bundlepath]; to get.

In the second method, we use NSData instead of NSString to read the index.html file because NSData is a binary byte array type. It does not have the concept of a character set, but it must be set in WebView, and the response method is LoadData:MIMEType:textEncodingName:baseURL:.

When you touch the Loadrequest button, WebView initiates an asynchronous call, and the Uiwebviewdelegate delegate protocol is used, as follows:

1-(Ibaction) Testloadrequest: (ID) Sender {2Nsurl * url = [Nsurl urlwithstring:@"http://www.51work6.com"];3Nsurlrequest * Request =[Nsurlrequest Requestwithurl:url];4 [Self.webview loadrequest:request];5Self.webview.Delegate=Self ;6 }7 8 #pragmaMark--Uiwebviewdelegate Delegate definition method9- (void) Webviewdidfinishload: (UIWebView *) WebView {TenNSLog (@"%@", [WebView stringbyevaluatingjavascriptfromstring:@"Document.body.innerHTML"]); One  A}

In the above method, we first create a Nsurl object, make the URL to be requested, the URL must be a strict HTTP format, and then build the Nsurlrequest object. Once the Nsurlrequest object is obtained, an asynchronous request can be initiated through the WebView Loadrequest: method. Asynchronous calls do not cause the main thread to clog and get a better user experience.

The uiwebviewdelegate definition of a delegate agreement is as follows:

WebView:shouldStartLoadWithRequest:navigationType:. This method is called before WebView starts loading a new interface and can be used to capture the javascipt of WebView.

Webviewdidstartload. This method is called after WebView begins loading a new interface.

Webviewdidfinishload. This method is called after WebView completes loading a new interface.

Webview:didfailloadwitherror. This method is called when the webview load fails.

Reading notes-uiview and controls

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.