iOS Development UI Article-a Brief Introduction to multiple controllers and navigation controllers

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>iOS Development UI Article-a Brief Introduction to multiple controllers and navigation controllers</strong></p></p><p><p>one, Multi-controller</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">An iOS app rarely consists of a single controller, unless the app is extremely simple. When there are multiple controllers in the app, we need to manage these controllers</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">When there are multiple view, you can use a large view to manage 1 or more small view, the controller is the same, with 1 controllers to manage the other multiple controllers</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">For example, use a controller A to manage 3 controllers b, C, D. Controller A is referred to as the "parent controller" of controller b, c, d, and the "sub-controller" of controller b, c, d, which is called Controller A.</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">To facilitate the management of the controller, iOS offers 2 more special controllers</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Uinavigationcontroller</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Uitabbarcontroller</p></p><p><p>second, Navigation Controller</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">With uinavigationcontroller, you can easily manage multiple controllers and easily switch between controllers, typically the System's own "settings" app</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"></p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p><p>third, the use of uinavigationcontroller steps</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">(1) Initialize Uinavigationcontroller</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">(2) set UIWindow Rootviewcontroller to Uinavigationcontroller</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">(3) depending on the situation, add the corresponding number of sub-controllers by the push method</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><span style="color: #008080;">1<span style="color: #0000ff;">#import<span style="color: #800000;">"<span style="color: #800000;">YYAppDelegate.h<span style="color: #800000;">"<span style="color: #008080;">2<span style="color: #0000ff;">#import<span style="color: #800000;">"<span style="color: #800000;">YYOneViewController.h<span style="color: #800000;">"<span style="color: #008080;">3<span style="color: #008080;">4<span style="color: #0000ff;">@implementation<span style="color: #000000;">Yyappdelegate<span style="color: #008080;">5<span style="color: #008080;">6-(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *<span style="color: #000000;">) launchoptions<span style="color: #008080;">7<span style="color: #000000;">{<span style="color: #008080;">8 Self.window =<span style="color: #000000;">[[uiwindow alloc] Initwithframe:[[uiscreen mainscreen] bounds];<span style="color: #008080;">9 Self.window.backgroundColor =<span style="color: #000000;">[uicolor whitecolor];<span style="color: #008080;">10<span style="color: #008080;">11<span style="color: #008000;">//<span style="color: #008000;">1. Create a navigation controller<span style="color: #008080;">Uinavigationcontroller *nav=<span style="color: #000000;">[[uinavigationcontroller alloc]init];<span style="color: #008080;">13<span style="color: #008000;">//<span style="color: #008000;">2. Set the navigation controller as the root view of window<span style="color: #008080;">Self.window.rootviewcontroller=<span style="color: #000000;">Nav<span style="color: #008080;">15<span style="color: #008080;">16<span style="color: #008080;">17<span style="color: #008000;">//<span style="color: #008000;">3. Adding a child controller to the navigation controller<span style="color: #008080;">18<span style="color: #008000;">//<span style="color: #008000;">Create some controllers<span style="color: #008080;">Uiviewcontroller *c1=<span style="color: #000000;">[[uiviewcontroller alloc]init];<span style="color: #008080;">20<span style="color: #008000;">//<span style="color: #008000;">Set C1 view color for this controller<span style="color: #008080;">C1.view.backgroundcolor=<span style="color: #000000;">[uicolor redcolor];<span style="color: #008080;">22<span style="color: #008080;">Uiviewcontroller *c2=<span style="color: #000000;">[[uiviewcontroller alloc]init];<span style="color: #008080;">C2.view.backgroundcolor=<span style="color: #000000;">[uicolor purplecolor];<span style="color: #008080;">25<span style="color: #008080;">Uiviewcontroller *c3=<span style="color: #000000;">[[uiviewcontroller alloc]init];<span style="color: #008080;"><span style="color: #008080;">27 c3.view.backgroundcolor=<span style="color: #000000;">[uicolor browncolor];< Span style= "color: #008080;" >28 <span style="color: #008080;">29 <span style="color: #008000;">//<span style="color: #008080;" add these controllers to the navigation controller <span>30 <span style="color: #000000;"> [nav pushviewcontroller:c1 animated : YES]; <span style="color: #008080;">31 <span style="color: #000000;"> [nav pushviewcontroller:c2 Animated:yes];<span style="color: #008080;">32 <span style="color: #000000;"> [nav pushviewcontroller:c3 Animated:yes];<span style="color: #008080;" ] <span>34 <span style="color: #000000;"> [self.window Makekeyandvisible];<span style="]" color: #008080; ">35 <span style="color: #0000ff;">return<span style="color: #000000;"> Yes;<span style="color: #008080;"> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p style="margin-left: 30px;"><p style="margin-left: 30px;">Running the emulator, you can see a primitive with three sub-controllers managing the Page.</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">But now we can only have an interface, we do not need to create three controller at a time to wait Here.</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Requirements: create three sub-controllers, each with a button on the View's interface, click to jump to the next Screen.</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Implementation (complete three pages with a simple jump via buttons):</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"><span>Note: the creation of the first Sub-controller is written in the proxy method.</span></p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">YYAPPDELEGATE.M File Code</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><span style="color: #008080;">1<span style="color: #008000;">//<span style="color: #008080;">2<span style="color: #008000;">//<span style="color: #008000;">Yyappdelegate.m<span style="color: #008080;">3<span style="color: #008000;">//<span style="color: #008000;">01-use of the Navigation controller 1<span style="color: #008080;">4<span style="color: #008000;">//<span style="color: #008080;">5<span style="color: #008000;">//<span style="color: #008000;">Created by Apple on 14-6-4.<span style="color: #008080;">6<span style="color: #008000;">//<span style="color: #008000;">Copyright (c) 2014 Itcase. All Rights Reserved.<span style="color: #008080;">7<span style="color: #008000;">//<span style="color: #008080;">8<span style="color: #008080;">9<span style="color: #0000ff;">#import<span style="color: #800000;">"<span style="color: #800000;">YYAppDelegate.h<span style="color: #800000;">"<span style="color: #008080;">10<span style="color: #0000ff;">#import<span style="color: #800000;">"<span style="color: #800000;">YYOneViewController.h<span style="color: #800000;">"<span style="color: #008080;">11<span style="color: #008080;">12<span style="color: #0000ff;">@implementation<span style="color: #000000;">Yyappdelegate<span style="color: #008080;">13<span style="color: #008080;">-(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *<span style="color: #000000;">) launchoptions<span style="color: #008080;">15<span style="color: #000000;">{<span style="color: #008080;">Self.window =<span style="color: #000000;">[[uiwindow alloc] Initwithframe:[[uiscreen mainscreen] bounds];<span style="color: #008080;">Self.window.backgroundColor =<span style="color: #000000;">[uicolor whitecolor];<span style="color: #008080;">18<span style="color: #008080;">19<span style="color: #008000;">//<span style="color: #008000;">1. Create a navigation controller<span style="color: #008080;">Uinavigationcontroller *nav=<span style="color: #000000;">[[uinavigationcontroller alloc]init];<span style="color: #008080;">21st<span style="color: #008000;">//<span style="color: #008000;">2. Set the navigation controller as the root view of window<span style="color: #008080;">Self.window.rootviewcontroller=<span style="color: #000000;">Nav<span style="color: #008080;">23<span style="color: #008080;">24<span style="color: #008080;">25<span style="color: #008000;">//<span style="color: #008000;">3. Adding a child controller to the navigation controller<span style="color: #008080;">Yyoneviewcontroller *one=<span style="color: #000000;">[[yyoneviewcontroller alloc]init];<span style="color: #008080;">27<span style="color: #000000;">[nav Pushviewcontroller:one animated:yes];<span style="color: #008080;">28<span style="color: #008080;">29<span style="color: #000000;">[self.window makekeyandvisible];<span style="color: #008080;">30<span style="color: #0000ff;">Return<span style="color: #000000;">YES;<span style="color: #008080;">31<span style="color: #008080;">32<span style="color: #008080;">33<span style="color: #008000;">//<span style="color: #008000;">//<span style="color: #008000;">Create some controllers<span style="color: #008080;">34<span style="color: #008000;">//<span style="color: #008000;">Uiviewcontroller *c1=[[uiviewcontroller alloc]init];<span style="color: #008080;">35<span style="color: #008000;">//<span style="color: #008000;">//<span style="color: #008000;">Set C1 view color for this controller<span style="color: #008080;">36<span style="color: #008000;">//<span style="color: #008000;">C1.view.backgroundcolor=[uicolor redcolor];<span style="color: #008080;">37<span style="color: #008000;">//<span style="color: #008080;">38<span style="color: #008000;">//<span style="color: #008000;">Uiviewcontroller *c2=[[uiviewcontroller alloc]init];<span style="color: #008080;">39<span style="color: #008000;">//<span style="color: #008000;">C2.view.backgroundcolor=[uicolor purplecolor];<span style="color: #008080;">40<span style="color: #008000;">//<span style="color: #008080;">41<span style="color: #008000;">//<span style="color: #008000;"><span style="color: #008000;"> Uiviewcontroller *c3=[[uiviewcontroller alloc]init]; <span style="color: #008080;">42 <span style="color: #008000;">//<span style="color: #008080;" c3.view.backgroundcolor="[uicolor" browncolor]; <span>43 <span style="color: #008000;">//<span style="color: #008080;">44 <span style=" Color: #808080; ">///<span style="color: #008000;">/add These controllers to the navigation controller <span style="color: #008080;">45 <span style="color: #008000;">//<span style="color: #008000;"> [nav pushviewcontroller:c1 Animated:yes];<span style="color: #008080;">46 <span style="color: #008000;">//<span style="color: #008000;"> [nav pushviewcontroller:c2 Animated:yes];<span style="color: #008080;">47 <span style="color: #008000;">//<span style="color: #008000;"> [nav pushviewcontroller:c3 Animated:yes];<span style="color: #008080;">48} </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p style="margin-left: 60px;"><p style="margin-left: 60px;">Create three child control classes and their corresponding xib files</p></p><p style="margin-left: 90px;"><p style="margin-left: 90px;">YYONEVIEWCONTROLLER.M file</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><span style="color: #008080;">1<span style="color: #008000;">//<span style="color: #008080;">2<span style="color: #008000;">//<span style="color: #008000;">Yyoneviewcontroller.m<span style="color: #008080;">3<span style="color: #008000;">//<span style="color: #008000;">01-use of the Navigation controller 1<span style="color: #008080;">4<span style="color: #008000;">//<span style="color: #008080;">5<span style="color: #008000;">//<span style="color: #008000;">Created by Apple on 14-6-4.<span style="color: #008080;">6<span style="color: #008000;">//<span style="color: #008000;">Copyright (c) 2014 Itcase. All Rights Reserved.<span style="color: #008080;">7<span style="color: #008000;">//<span style="color: #008080;">8<span style="color: #008080;">9<span style="color: #0000ff;">#import<span style="color: #800000;">"<span style="color: #800000;">YYOneViewController.h<span style="color: #800000;">"<span style="color: #008080;">10<span style="color: #0000ff;">#import<span style="color: #800000;">"<span style="color: #800000;">YYTwoViewController.h<span style="color: #800000;">"<span style="color: #008080;">11<span style="color: #008080;">12<span style="color: #0000ff;">@interface<span style="color: #000000;">Yyoneviewcontroller ()<span style="color: #008080;">13<span style="color: #008000;">/*<span style="color: #008000;">*<span style="color: #008080;">14<span style="color: #008000;">Jump to the second interface<span style="color: #008080;">15<span style="color: #008000;">*/<span style="color: #008080;">-(ibaction) Jump2two: (<span style="color: #0000ff;">Id<span style="color: #000000;">) sender;<span style="color: #008080;">17<span style="color: #008080;">18<span style="color: #0000ff;">@end<span style="color: #008080;">19<span style="color: #008080;">20<span style="color: #0000ff;">@implementation<span style="color: #000000;">Yyoneviewcontroller<span style="color: #008080;">21st<span style="color: #008080;">22<span style="color: #008080;">-(ibaction) Jump2two: (<span style="color: #0000ff;">Id<span style="color: #000000;">) Sender {<span style="color: #008080;">24<span style="color: #008000;"><span style="color: #008000;">//<span style="color: #008000;">1. Create a second sub-controller <span style="color: #008080;">25 Yytwoviewcontroller *two=<span style="color: #000000;">[[yytwoviewcontroller alloc]init];<span style="color: #008080;"> <span style="color: #008080;">27 <span style="color: #008000;">//<span style="color: #008000;">2. The handle controller is added to the navigation controller <span style="color: #008080;">28 <span style="color: #008000;">//<span style="color: #008080;" is there any way to get the navigation controller? <span>29 <span style="color: #008000;">//<span style="color: #008080;" as long as the current controller is a child of navigation controller, you can get directly to where located <span>30 <span style="color: #000000;"> [ Self.navigationcontroller Pushviewcontroller:two animated:yes]; <span style="color: #008080;">31 <span style="color: #000000;">}<span style="color: #008080;">32 <span style=" Color: #0000ff; "> @end </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p style="margin-left: 60px;"><p style="margin-left: 60px;">YYTWOVIEWCONTROLLER.M file</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><span style="color: #008080;">1<span style="color: #008000;">//<span style="color: #008080;">2<span style="color: #008000;">//<span style="color: #008000;">Yytwoviewcontroller.m<span style="color: #008080;">3<span style="color: #008000;">//<span style="color: #008000;">01-use of the Navigation controller 1<span style="color: #008080;">4<span style="color: #008000;">//<span style="color: #008080;">5<span style="color: #008000;">//<span style="color: #008000;">Created by Apple on 14-6-4.<span style="color: #008080;">6<span style="color: #008000;">//<span style="color: #008000;">Copyright (c) 2014 Itcase. All Rights Reserved.<span style="color: #008080;">7<span style="color: #008000;">//<span style="color: #008080;">8<span style="color: #008080;">9<span style="color: #0000ff;">#import<span style="color: #800000;">"<span style="color: #800000;">YYTwoViewController.h<span style="color: #800000;">"<span style="color: #008080;">10<span style="color: #0000ff;">#import<span style="color: #800000;">"<span style="color: #800000;">YYThreeViewController.h<span style="color: #800000;">"<span style="color: #008080;">11<span style="color: #0000ff;">@interface<span style="color: #000000;">Yytwoviewcontroller ()<span style="color: #008080;">-(ibaction) Jump2three: (<span style="color: #0000ff;">Id<span style="color: #000000;">) sender;<span style="color: #008080;">13<span style="color: #008080;">14<span style="color: #0000ff;">@end<span style="color: #008080;">15<span style="color: #008080;">16<span style="color: #0000ff;">@implementation<span style="color: #000000;">Yytwoviewcontroller<span style="color: #008080;">17<span style="color: #008080;">18<span style="color: #008000;"><span style="color: #008000;">//<span style="color: #008000;"> Jump to the third Sub-controller <span style="color: #008080;">19-( Ibaction) jump2three: (<span style="color: #0000ff;">id<span style="color: # 008080; " ] sender {<span>20 <span style="color: #008000;">//<span style="color: #008000;">1. Create a third child controller <span style="color: #008080;">21 Yythreeviewcontroller *three=<span style="color: #000000;">[[yythreeviewcontroller alloc]init];<span style="color : #008080; ">22 <span style="color: #008000;">//<span style="color: #008000;">2. Adding a child controller to the navigation controller <span style="color: #008080;">23 <span style="color: #000000;"> [self.navigationcontroller pushviewcontroller: Three animated:yes]; <span style="color: #008080;">24 <span style="color: #008080;">25 <span style="color: #000000;">}<span style=" Color: #008080; ">26 <span style="color: #0000ff;"> @end </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p style="margin-left: 60px;"><p style="margin-left: 60px;"><span>Tip: as long as the current controller is a child controller of the navigation controller, you can get directly to the navigation controller where the current controller is located through the Self.navigationcontroller property</span></p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Project file structure and run Effect:</p></p><p style="margin-left: 90px;"><p style="margin-left: 90px;"></p></p><p><p>iOS Development UI Article-a Brief Introduction to multiple controllers and navigation controllers</p></p></span>

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.