iOS Development View Controller (Uiviewcontroller)

Source: Internet
Author: User

The view controller should play the role of Control layer (C) in the MVC design pattern, Uiviewcontroller's responsibility to manage the view associated with it internally, and communicate and coordinate with other uiviewcontroller externally. A view controller manages a view, which can have a child view, and its view property points to the views it manages. The Uiviewcontroller class can have subclasses that can use a uiviewcontroller subclass of a system or create a subclass of Uiviewcontroller directly on its own.

Use code to create controllers and views.

Start creating a window-based empty application project.

Create a View controller subclass: File–new–new File (command+n) and select Objective-c class, named Rootviewcontroller and then subclass Of the input Uiviewcontroller, click the Next button to save.

Now there is a Rootviewcontroller class, and then edit its code. A view controller is responsible for acquiring or creating its own view. If the view controller creates its view manually, you must overload the Loadview method of the Uiviewcontroller class. Set a color for the view below and put a "Hello world! "tab is in this view.

@interface Rootviewcontroller ()@end@implementation Rootviewcontroller-(void)Loadview{ Applicationframe is the entire visible area, not including the status bar UIView*View= [[UIViewAlloc]initWithFrame:[[UIScreenMainscreen]Applicationframe]]; Set the color of the viewView.BackgroundColor= [UicolorGreencolor]; Add a Label UILabel*Label= [[UILabelAlloc]Init];Label.Text[Email protected]"Hello world!"; Self-adapting size ; CenterLabel.Center= Cgpointmake(Cgrectgetmidx(Viewbounds),  cgrectgetmidy  (view. Bounds //added to View [view Addsubview:label];//settings Self.view = View Controller can manage this view, if implemented Loadview then you must set Self.view self.= View;} ...  @end             

The view controller is ready to use it now. We'll tweak the application's delegate class (Appdelegate), declaring the view controller properties in the header file of the application delegate class.

@property(strong, nonatomic)rootviewcontroller*rootviewcontroller;  

In the implementation code of the application delegate, we import "RootViewController.h" and create a new view controller in the application delegate's Application:didfinishlaunchingwithoptions method. And put the view in the interface.

- (BOOL)Application:(UIApplication *)Application Didfinishlaunchingwithoptions:(Nsdictionary *)Launchoptions{ Self.Window= [[UIWindowAlloc]initWithFrame:[[UIScreenMainscreen]Bounds]]; Override point for customization after application launch. Self.Window.BackgroundColor= [UicolorWhitecolor]; Create a View Controller Rootviewcontroller*Thervc= [[RootviewcontrollerAlloc]Init]; self.= Thervc;//not only to create a view, but also to put the view in the interface  [self window addsubview:self. Rootviewcontroller. View [self. Window Makekeyandvisible return Yes;               /span>                

Compile and run the application, and run the results as shown.

There is also an easy way to use the Rootviewcontroller property of UIWindow. This makes it unnecessary to declare the view controller properties in the header file of the application delegate class. You do not need to add the view to the window as a child view. When assigning an Uiviewcontroller instance to a Rootviewcontroller property, it automatically gets the Uiviewcontroller view and makes it the only child view of the window.

- (BOOL)Application:(UIApplication *)Application Didfinishlaunchingwithoptions:(Nsdictionary *)Launchoptions{ Self.Window= [[UIWindowAlloc]initWithFrame:[[UIScreenMainscreen]Bounds]]; Override point for customization after application launch. Self.Window.BackgroundColor= [UicolorWhitecolor]; Create a View Controller Rootviewcontroller*Thervc= [[RootviewcontrollerAlloc]Init //Self.rootviewcontroller = Thervc; //not only to create a view, but also to put the view in the interface  //[self.window addsubview: Self.rootViewController.view];  //this line of code has the same effect as the two lines of code commented out above.  self.. Rootviewcontroller = Thervc;[self. return Yes;               /span>               

The root view controller of the window is globally available, and if you need to get the root view controller, you can use the following code:

Uiviewcontroller*=[[[uiapplication sharedapplication] Keywindow]  Rootviewcontroller];         

Create a view controller in the nib file

Designing and maintaining a complex user interface in the nib file is easier and simpler than creating it in code.

Command+n–user Interface–view, named View.xib

Edit View.xib, change the class of file ' s owner to Rootviewcontroller. This will generate a view output and connect it to the view. (Right-click on file's owner, click View, then drag to view.) )

Then modify the relevant code for the application delegate:

- (BOOL)Application:(UIApplication *)Application Didfinishlaunchingwithoptions:(Nsdictionary *)Launchoptions{ Self.Window= [[UIWindowAlloc]initWithFrame:[[UIScreenMainscreen]Bounds]]; Override point for customization after application launch. Self.Window.BackgroundColor= [UicolorWhitecolor]; Create a view controller using the nib file Rootviewcontroller*Thervc= [[rootviewcontroller Alloc ]initwithnibname:@ "View"  Bundle:nil]; self.. Rootviewcontroller = Thervc;[self. return Yes;               /span>                

initialization of the Uiviewcontroller
    • The program requests the Controller's view
    • If the view is in memory, it is loaded directly. Conversely, if it does not exist, Uiviewcontroller calls the Loadview method
    • The Loadview method performs the following actions:
      • If you overload this method, you must create the necessary view and pass a non-nil value to the Uiviewcontroller view property.
      • If you do not overload this function, Uiviewcontroller will use Uiviewcontroller's Nibname and Nibbundle properties to attempt to load the view from the nib file by default. If the nib file is not found, Viewcontroller will find the nib associated with it in the following two steps.
        A if the class name contains a controller, for example, the class name of Viewcontroller is Myviewcontroller, the lookup exists for myview.nib;
        B find the same file as the Viewcontroller class name, such as Myviewcontroller, to find out if there is a myviewcontroller.nib.
      • If there is no nib file available, then it creates an empty uiview as its view.
    • Uiviewcontroller calls Viewdidload to perform some load-time tasks.

When the view property needs to be displayed or accessed, the VC calls the Loadview method, at which point it creates a view and assigns it to the Vc.view property. Then the VC Viewdidload method is called, this time the Vc.view guarantee is a value, you can do further initialization operations, such as adding some subview. Note: If you override the Loadview method when customizing the VC, you do not need to call the [Super Loadview] method.

Uiviewcontroller The steps to uninstall view.

The view,uiviewcontroller associated with it always loads the view when it is needed and unloads the view when it is not needed, so it also assumes responsibility for managing the application resources. Understand the life cycle of Uiviewcontroller (LifeCycle) to effectively manage application resources. The controller's view is best to load when it needs to be displayed, and to release the required view and related data objects when the system issues a memory warning. Uiviewcontroller the steps to uninstall view are as follows:

    • Program receives memory warning
    • Each Uiviewcontroller calls didreceivememorywarning, and the view is safely released by default
    • If Uiviewcontroller releases its view, it will call Viewdidunload. This method can be overloaded to perform additional cleanup work.

Official document: The View Controller life Cycle

iOS Development View Controller (Uiviewcontroller)

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.