"iOS base Control-15"-Uinavigationcontroller multiple controllers 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, a parent view is used to manage multiple sub-view4. The same is true for controller management, where a parent controller is used to control the controller, the system provides two controllers for easy management
    • 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 window 3     self.window = [[UIWindow alloc] init]; 4     self.window.frame = [[UIScreen mainscreen] bounds]; 5< C3/>self.window.backgroundcolor = [Uicolor Graycolor]; Window is a gray background 6     [Self.window makekeyandvisible]; 7     8     9     //Set UINAVIGATIONCONTROLLER10     Uinavigationcontroller *nvcontroller = [[Uinavigationcontroller alloc] init];11     NvController.view.backgroundColor = [Uicolor Bluecolor]; Set Blue background     Self.window.rootViewController = nvcontroller;13     return yes;15}
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     //configuration of the first sub-controller 2     Viewcontrollerone *vc1 = [[Viewcontrollerone alloc] init]; 3     [Nvcontroller PUSHVIEWCONTROLLER:VC1 Animated:yes]; 4     5     //Configure a second sub-controller 6     viewcontrollertwo *VC2 = [[Viewcontrollertwo alloc] init]; 7     [Nvcontroller PUSHVIEWCONTROLLER:VC2 Animated:yes]; 8     9     //Configure the third sub-controller, which is the top controller of the stack     viewcontrollerthree *vc3 = [[Viewcontrollerthree alloc] init];11     [ 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     //must be loaded after activating the app, all view size positions can be determined 3     Uinavigationcontroller *nvcontroller = (Uinavigationcontroller *) self.window.rootviewcontroller;4     NSLog (@ "%@", Nsstringfromcgrect (NvController.view.frame)); Frame5 NSLog for the entire navigation controller     (@ "%@", Nsstringfromcgrect (NvController.navigationBar.frame));//navigation bar Frame6}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 @property (nonatomic,copy) Nsarray *viewcontrollers; The current view controller stack.
3.push Sub-Controller, replace View on window, display always view of top controller
1-(void) Pushviewcontroller: (Uiviewcontroller *) Viewcontroller animated: (BOOL) animated; 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     //Configure first sub-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     //Configure first sub-controller 2     Viewcontrollerone *vc1 = [[Viewcontrollerone alloc] init];3     nvcontroller.viewcontrollers = @[vc1];
(4) Specify Rootviewcontroller when initializing
1     //pass in a viewcontroller as Nvcontroller rootViewController2     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     //Configure third sub-controller 3     Viewcontrollerthree *vc3 = [[Viewcontrollerthree alloc] init]; 4     [s Elf.navigationcontroller PUSHVIEWCONTROLLER:VC3 Animated:yes]; 5} 6  7-(ibaction) Gotofour {8     //configuration Fourth sub-controller 9     Viewcontrollerfour *vc4 = [[Viewcontrollerfour alloc] Init];10     [Self.navigationcontroller Pushviewcontroller:vc4 animated:yes];11}
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 of the View2-(ibaction) Backtoone {3//     actually not get the first controller VC1 reference 4//    [ Self.navigationcontroller poptoviewcontroller:vc1 animated:yes];5    6     //eject directly until the navigator's Rootviewcontroller, 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     self.navigationItem.title = @ "First view"; 4}
(2) Leftbarbuttonitem/leftbarbuttonitems, will overwrite the "Back" button (3) Rightbarbuttonitem/rightbarbuttonitems
1-(void) Viewdidload {2     [Super Viewdidload]; 3     self.navigationItem.title = @ "Second Controller"; 4     5//     Set the item 6     self.navigationItem.leftBarButtonItem = [[Uibarbuttonitem alloc] initwithtitle:@ "I want to go back" style in the upper left corner: Uibarbuttonitemstyleplain Target:nil Action:nil]; 7     8     //Set the item 9 in the upper right corner     //Use the bar button     uibarbuttonitem *item1 = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemcompose target:nil action:nil];11     //Use custom VIEW13     Uibarbuttonitem *item2 = [[Uibarbuttonitem alloc] Initwithcustomview:[uibutton buttonwithtype: UIBUTTONTYPECONTACTADD]];14     //This is this is a right-to-left arrangement showing     self.navigationItem.rightBarButtonItems = @[item1 , item2];17}
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     //must be loaded after activating the app, all view size positions can be determined 3     Uinavigationcontroller *nvcontroller = (Uinavigationcontroller *) self.window.rootviewcontroller;4     NSLog (@ "%@", Nsstringfromcgrect (NvController.view.frame)); Frame5 NSLog for the entire navigation controller     (@ "%@", Nsstringfromcgrect (NvController.navigationBar.frame));//navigation bar Frame6}
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-15"-Uinavigationcontroller multiple controllers 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.