[iOS base Control-6.11.2]-Uinavigationcontroller Multi-controller simple to use

Source: Internet
Author: User

A. Concept1. Usually one app has multiple controllers 2. These controllers need to be managed 3. When there are multiple view, use a parent view to manage multiple sub-view 4. The same is true for controller management, to a parent controller, to control the sub-controller to facilitate the management of the controller, the system provides two controllers
    • Uinavigationcontroller
    • Uitabbarcontroller
Use of B.uinavigationcontrollerUinavigationcontroller Steps to use
Initialize Uinavigationcontroller
Set UIWindow Rootviewcontroller to Uinavigationcontroller depending on the situation, add the corresponding number of sub-controllers by the push method  1. Initialize 2. Set to Rootviewcontroller
1-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {2     //Set Window3Self.window =[[UIWindow alloc] init];4Self.window.frame =[[UIScreen mainscreen] bounds];5Self.window.backgroundColor = [Uicolor Graycolor];//window is a gray background6 [Self.window makekeyandvisible];7    8    9     //Set UinavigationcontrollerTenUinavigationcontroller *nvcontroller =[[Uinavigationcontroller alloc] init]; OneNvController.view.backgroundColor = [Uicolor Bluecolor];//Set Blue background ASelf.window.rootViewController =Nvcontroller; -     -     returnYES; the}
3. With the push method, add a sub-controller (1) to create 3 controllers and a bound Xib file (2) a sub-controller stack for push-in Navigationcontroller
1     //Configuring the first child controller2Viewcontrollerone *VC1 =[[Viewcontrollerone alloc] init];3 [Nvcontroller pushviewcontroller:vc1 animated:yes];4    5     //Configure a second sub-controller6Viewcontrollertwo *VC2 =[[Viewcontrollertwo alloc] init];7 [Nvcontroller pushviewcontroller:vc2 animated:yes];8    9     //Configure a third sub-controller, which is the top of the stackTenViewcontrollerthree *VC3 =[[Viewcontrollerthree alloc] init]; One[Nvcontroller pushviewcontroller:vc3 Animated:yes];
(3) The view of the stack top controller is displayed. C. Management principle of sub-controller1. The navigation bar is under the status bar (height 20), height 44
1- (void) Applicationdidbecomeactive: (UIApplication *) Application {2     //after activating the app, all view size locations must be determined3Uinavigationcontroller *nvcontroller = (Uinavigationcontroller *) Self.window.rootViewController;4NSLog (@"%@", Nsstringfromcgrect (NvController.view.frame));//frame of the entire navigation controller5NSLog (@"%@", Nsstringfromcgrect (NvController.navigationBar.frame));//frame of the navigation bar6 }7  
Out navigationcontroller[2669:183137] {{0, 0}, {480}} 2014-12-19 21:16:30.413 navigationcontroller[2669:183137] {{0, +}, {[]2.UINavigationController has a sub-controller stack, using the Push method is to add a sub-controller to the stack
1 // The current view controller stack.
3.push Sub-Controller, replace View on window, display always view of top controller
1 -(void//  Uses a horizontal slide transition. Have no effect if the view controller is already in the stack.
  D. Operation of the sub-controller stack1. Stack and add sub-controller (1) use "Pushviewcontroller"
1     // Configuring the first child controller 2     Viewcontrollerone *VC1 = [[Viewcontrollerone alloc] init]; 3     [Nvcontroller pushviewcontroller:vc1 Animated:yes];
(2) Use "Addchildcontroller"
    • The "Viewcontrollers" member in Uinavigationcontroller is an array, which is the sub-controller stack
    • The "childviewcontrollers" function is the same (this is the member property of Uiviewcontroller)
    • The same function as "Addchildcontroller" and "Pushviewcontroller" (This is the Uiviewcontroller method)
Configuring the first child controller
1     Viewcontrollerone *VC1 = [[Viewcontrollerone alloc] init]; 2     [Nvcontroller ADDCHILDVIEWCONTROLLER:VC1];
(3) directly into the viewcontrollers array
1     // Configuring the first child controller 2     Viewcontrollerone *VC1 = [[Viewcontrollerone alloc] init]; 3     Nvcontroller.viewcontrollers = @[vc1];
(4) Specify Rootviewcontroller when initializing
1     // Pass in a viewcontroller as Nvcontroller Rootviewcontroller 2     Nvcontroller = [[Uinavigationcontroller alloc] initwithrootviewcontroller:vc1];
Note: Do not mix "pushviewcontroller" and "Addchildviewcontroller", in some cases there will be no controller added to the stack. And it was chaotic and totally unpredictable. (5) Use events to stack a. View of the second controller plus two buttons to jump to the third and fourth controller view B. Click event method Each controller has a Navigationcontroller property that notifies Navigationcontroller to operate
1-(ibaction) gotothree {2     //Configuring a third child controller3Viewcontrollerthree *VC3 =[[Viewcontrollerthree alloc] init];4 [Self.navigationcontroller pushviewcontroller:vc3 animated:yes];5 }6 7-(ibaction) gotofour {8     //Configuring a fourth child controller9Viewcontrollerfour *vc4 =[[Viewcontrollerfour alloc] init];Ten [Self.navigationcontroller pushviewcontroller:vc4 animated:yes]; One}
2. Active out stack (1) use "Popviewcontrolleranimated:yes" to eject a controller
1 // here is the third one, to go back to the second 2 - (ibaction) backtotwo {3    [Self.navigationcontroller popviewcontrolleranimated : YES]; 4 }
(2) Use "Poptoviewcontroller" eject the controller until the specified controller (3) uses "Poptorootviewcontroller" to eject the controller inside the account until Rootviewcontroller, back to the bottom of the stack
1 // now is the third controller view, to go back to the first controller's view 2 - (ibaction) backtoone {3     //  In fact, there is no reference to the first controller VC1 4/ /    [Self.navigationcontroller poptoviewcontroller:vc1 animated:yes]; 5    6     // eject directly, until the rootviewcontroller of the Navigator, is the first sub-controller VC1 7     [Self.navigationcontroller Poptorootviewcontrolleranimated:yes]; 8 }
E. Setting the navigation bar1. The content of the navigation bar is controlled by the top controller 2. Use Navigationitem to control the contents of the navigation bar (1) title Title/titleview (This is the member property of Uiviewcontroller)
1 @property (nonatomic,copy) nsstring *title;  // localized title for use by a parent controller.
Sample: Set the title of the first controller
1 -(void) viewdidload {2    [Super Viewdidload]; 3     @" First View " ; 4 }
(2) Leftbarbuttonitem/leftbarbuttonitems, will overwrite the "Back" button (3) Rightbarbuttonitem/rightbarbuttonitems
1- (void) Viewdidload {2 [Super Viewdidload];3Self.navigationItem.title =@"a second controller";4    5 //set the item in the upper-left corner6Self.navigationItem.leftBarButtonItem = [[Uibarbuttonitem alloc] Initwithtitle:@"I'm going back ."style:uibarbuttonitemstyleplain Target:nil Action:nil];7    8     //set the item in the upper-right corner9     //using the bar buttonTenUibarbuttonitem *item1 =[[Uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemcompose target:nil Action:nil]; One     A     //using a custom view -Uibarbuttonitem *item2 =[[Uibarbuttonitem alloc] Initwithcustomview:[uibutton Buttonwithtype:uibuttontypecontactadd]]; -     the     //This is the display that is arranged from right to left -Self.navigationItem.rightBarButtonItems =@[item1,item2]; -}
Note: The leftbarbuttonitems is displayed in the order of the array from left to right, while Rightbarbuttonitems is from right to left for example, the items on the right of the top are placed on the left side:  Self.navigationItem.leftBarButtonItems = @[item1, item2]; 3. After the previous title has been set, if Leftbarbuttonitem (s) is not overwritten, the text of the Back button is automatically replaced with the title of the previous level, and The text displayed in the upper-left corner is controlled by the upper-level controller Navigationitem.backbarbuttonitem
 1  //  Second controller  2 -(void  ) viewdidload { 3   [super Viewdidload];  4   ...  5  //  Set the return style of the second controller  6  self.navigationItem.backBarButtonItem = [[ Uibarbuttonitem alloc] Initwithtitle:@ "  quick return 2   "  Style:uibarbuttonitemstyleplain target : nil Action:nil];  7 } 
4.navigatonBar is the entire navigation bar Nvcontroller.navigationbar when everything is loaded, the frame of the navigation bar can be initialized
1- (void) Applicationdidbecomeactive: (UIApplication *) Application {2     //after activating the app, all view size locations must be determined3Uinavigationcontroller *nvcontroller = (Uinavigationcontroller *) Self.window.rootViewController;4NSLog (@"%@", Nsstringfromcgrect (NvController.view.frame));//frame of the entire navigation controller5NSLog (@"%@", Nsstringfromcgrect (NvController.navigationBar.frame));//frame of the navigation bar6}
Out 2014-12-19 23:12:51.049 navigationcontroller[4214:236071] {{0, 0}, {$, 480}} 2014-12-19 23:12:51.050 navigationcontroller[4214:236071] {{0, +}, {[]

Last click-to-link graph:

F. Using storyboard navigation1. Create a basic navigation interface (1) Create a single view application, delete the own Viewcontroller Class (2) Delete the original Viewcontroller, drag into a navigationcontroller, and set to initial View Controller (3) Delete the Rootviewcontroller, drag into a Uiviewcontroller (4) Set the new Uiviewcontroller to the Navigator's Rootviewcontroller (5) Settings UIView, this is the first page double-click the corresponding position can edit the text (such as title), However, you cannot drag into the Barbuttonitems group using the storyboard method. (6) Drag into a uiview to Uiviewcontroller, and add some elements to run the effect: 2. Create a jump navigation function (1) Add two jump buttons to the view of the first controller, drag in two Uiviewcontroller (2) and drag to point, use " Push "event implementation effect: In the first controller interface, you can jump to the interface of the second or third controller Note: Do not create a back navigation button, in fact, re-created a controller to join the controller stack G. Life cycle of the controller1. Life cycle Method 2. Memory warning processing flow when there is not enough memory, priority recycling does not exist on the screen view so you should release the data in the Viewdidunload method

[iOS base Control-6.11.2]-Uinavigationcontroller Multi-controller simple to use

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.