iOS Development View and view Controller

Source: Internet
Author: User

View, view controller (Viewcontroller) is something important in the iOS Development UI section. While learning the view of this piece of stuff, the feeling is similar to the panel in Java Swing. There is a uiwindow in the Uikit framework to accommodate our view. Almost all visual controls in an application are instances of UIView and UIView subclasses, and UIWindow is also a subclass of UIView. UIWindow can be displayed on the screen without the help of the parent Class View, and the rest of the views need to be added to the parent view to display. The window is used to display the view, and below we will combine examples to learn more about view and Viewcontroller in iOS

1. First we need to build a emptyproject to test our view and Viewcontroller. The file structure of our empty project is as follows, we just need to add our view in the APPDELEGATE.M, or that sentence in order to better understand our view, all views are created and configured in code.

          

2. Before learning UIView we add a view to our emptyproject to see the effect, the above code is to add a uiwindow for our emptyproject, which is created by the system for us, The next uiiview we're going to place are all in window, typically there's only one window for each app, and of course some games have multiple application windows. The next piece of code is to add a main view to our window and cgrectmake to give us a new look.  CGRectMake (x, y, width, height); Configure the background color to greencolor and add it to our window at the end.

3. The interface is a view object that is laid out in an instance of the UIView class, UIView represents a rectangular area on the screen, is responsible for rendering the contents of the rectangular area, and responds to touch events that occur within that area. We can also think of a view as a view container, where you can add a sub-view above the view. The Subview added to the parent view will be placed in an array. The coordinates and index of the subview added to our Superview are configured relative to our parent view. We add a subview for the above view, and the code is as follows:

The results are as follows:

Here are some ways iOS provides a way to manage a child view, which is commonly used as follows:

(1) initWithFrame: Through frame initialization view, parameters are CGRectMake (x, y, width, height);

(2) Insertsubview:atindex: Inserts a view toward the specified layer, which view calls the method, and index is relative to WHO.

(3) Insertsubview:abovesubview: Inserts a child view on a view.

(4) Insertsubview:belowsubview: Add a new view after a child view

(5) Bringsubviewtofront: Put the handle view to the front

(6) Sendsubviewtoback: Put the handle view to the last

(7) Exchangesubviewatindex:withsubviewatindex: Swap two views in front and back order

(8) Removefromsuperview: Remove View from parent view

(9)-(void) Addsubview: (UIView *) view Add a

The hierarchy of views is distinguished by index, which increments from 0 with step 1, and index 0 represents the lowest level in the view hierarchy, the following is an introduction to the views of the official Apple document:

The view layer is broadly divided into the following categories

1. Container view

The container view is used to enhance the functionality of other views, or to provide additional visual separation for the contents of the view, such as the Uiscorllview class is used to display views that are too large to be displayed on a screen, that is, to automatically add scroll bars to the first diagram below. The UITableView class is a subclass of the Uiscrollview class for managing data lists, two, and other container views that are not listed here.

2. Controls

Control is used to create the user interface for most applications. A control is a special type of view that inherits the Uicontrol superclass, typically binding callback methods (such as Target-action callbacks and delegate callbacks) for user interaction. Controls include keystrokes, text boxes, sliders, and toggle switches. Some of the controls are as follows:

3. Display view

Controls and many other types of views provide interactive behavior, while others simply display information. The Uikit classes with this behavior include Uiimageview, UILabel, Uiprogressview, Uiactivityindicatorview; The following is the Uiprogressview display view


4. Text and Web views

Text and Web views provide a more advanced way for applications to display multiple lines of text. The Uitextview class supports displaying and editing multiple lines of text within a scrolling region, while the UIWebView class provides a way to display HTML content

5. Warning views and action forms

Warning views and action forms are used to instantly get the user's attention. The Uialertview class pops up a blue warning box on the screen, while the Uiactionsheet class outlines the action box from the bottom of the screen

6. Navigation view

The page labels and the navigation bar are used in conjunction with the view Controller to provide the user with a navigation tool from one screen to another. In use, you usually do not have to directly uitablebar and uinavigationbar items, but configure them with the appropriate controller interface or interface builder, Table bar and Navigation bar are as follows:

So much for the above view section, so how do our view controllers work? At the beginning of this article we added the View code is in the APPDELEGATE.M file Riga, actually did not do so, if we have been in the file above to instantiate our various controls, our application code will be very difficult to maintain. So how do we add a view controller to a emptyproject? There are so many pictures posted on it, let's go on to the code now!

1. We create a new view controller class Mainviewcontroller in an empty project, let Mainviewcontroller inherit from Uiviewcontroller, the contents of the MainViewController.h file are as follows:

123 #import <UIKit/UIKit.h>@interface MainViewController : UIViewController@end

2. We make our view declarations and instantiations in MAINVIEWCONTROLLER.M, with the following code:

1234567891011121314151617181920 #import "MainViewController.h"//用延展隐藏我们的组件@interface MainViewController ()@property (nonatomic, strong) UIView *subView;@end//-------实现部分-----------@implementation MainViewController//主视图加载后要做的事情-(void)viewDidLoad{    //实例化view并添加到mainView    self.subView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 150)];    self.subView.backgroundColor = [UIColor brownColor];    [self.view addSubview:self.subView];}@end

3. We need to associate our new View controller with our window, instantiate the Mainviewcontroller in APPDELEGATE.M and add it to the window with the following code:

12345678910111213 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];          //实例化MainViewController,并添加到window    MainViewController * mianViewController = [[MainViewController alloc] init];    [self.window addSubview:mianViewController.view];        returnYES;}

? ? This understanding of the view and view controller, with future in-depth learning will be updated and modify the blog at any time.

iOS Development View and view Controller

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.