iOS development from beginner to proficient--Navigation controller Basics

Source: Internet
Author: User

Navigation Controller Basics

Implement the navigation bar above, the left button, the middle title, the test button on the right, and a play button

Re-create a vcroot root view controller

#import "AppDelegate.h"#import "VCRoot.h"@interfaceAppdelegate ()@end@implementationappdelegate-(BOOL)Application:(uiapplication *) applicationdidfinishlaunchingwithoptions:(Nsdictionary *) launchoptions {//Override Point forCustomization after application launch. Self.window= [[UIWindow alloc]initWithFrame: [UIScreen mainscreen].bounds];//Create a root view controller vcroot * root = [[Vcroot alloc]init];//Creating a navigation controller//The navigation controller is primarily used to manage switching of multiple view controllers//Hierarchical way to manage multiple view controllers//When creating a controller, be sure to have a root view controller//Parameter one: is the root view controller as the navigation controller Uinavigationcontroller *nav = [[Uinavigationcontroller alloc]Initwithrootviewcontroller: Root];//WillwindowThe root view is set to the navigation controller self.window. Rootviewcontroller =nav; [Self.windowMakekeyandvisible];returnYES;} - (void)applicationwillresignactive:(UIApplication *) Application {//Sent whenThe application isAbout-to-move from active-to-inactive state. This can occur forCertain types ofTemporary interruptions (such as an incoming phone callorSMS message)or  whenThe user quits the application andIt begins the transition to the background state.//Use Thismethod to pause ongoing tasks, disable timers, andThrottle down OpenGL ES frame rates. Games should use Thismethod to pause the game.} - (void)Applicationdidenterbackground:(UIApplication *) Application {//Use Thismethod to release shared resources, save user data, invalidate timers, andStore enough application state information to restore your applicationinch  CaseIt isTerminated later.//If your application supports background execution, ThisMethod isCalled instead of applicationwillterminate: whenThe user quits.} - (void)Applicationwillenterforeground:(UIApplication *) Application {//Called as part ofThe transition from the background to the inactive state; Here you can undo many ofThe changes made onEntering the background.} - (void)applicationdidbecomeactive:(UIApplication *) Application {//Restart any tasks were paused (or  notYet started) whileThe application was inactive. If the application was previouslyinchThe background, optionally refresh the user interface.} - (void)applicationwillterminate:(UIApplication *) Application {//Called whenThe application isAbout to terminate. Save dataifappropriate. See AlsoApplicationdidenterbackground:.}@end

And then:

#import "VCRoot.h"  @interface vcroot ()@end @implementation vcroot - (void) Viewdidload {[SuperViewdidload];additional setup after loading the view.     Self. View. BackgroundColor=[UicolorYellowcolor];//Set title text for navigation bar     Self. Title[Email protected]"Root View";//Set the title of the navigation element item    //If Navigationitem.title is not set, nil    //The system will use Self.title as the title    //If SELF.NAVIGATIONITEM.TITL is not empty    //Set Self.navigationItem.titl as header content     Self. Navigationitem. Title[Email protected]"TITLE";//Create a button on the left side of the navigation bar    //Create a button according to the title text    //p1: Text on a button    //p2: Button style    //P3: Event owner    //P4: Button Event    Uibarbuttonitem*LEFTBTN = [[UibarbuttonitemAlloc] initwithtitle:@"left"Style:uibarbuttonitemstyledone Target: SelfAction@selector(Pressleft)];//Assign a value to the left button of a navigation element item     Self. Navigationitem. Leftbarbuttonitem=LEFTBTN;//Create a button on the right side of the navigation bar    //Create a button according to the system style    //Only to specify style style, the system-style button content or title text cannot be changed    //p1: Button style uibarbuttonsystemitemadd/uibarbuttonsystemitemplay ....    //P2: Event owner    //p3: Button Event    Uibarbuttonitem* RIGHTBTN = [[UibarbuttonitemAlloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemplay target: SelfAction@selector(Pressright)]; Self. Navigationitem. Rightbarbuttonitem=RIGHTBTN;//Tag Object    UILabel* Label =[[UILabelAlloc]initwithframe:cgrectmake (Ten,Ten, -, +)]; Label. Text[Email protected]"Test"; Label. TextAlignment=nstextalignmentcenter;//method to add any type of control to the navigation button    Uibarbuttonitem* Item03 = [[UibarbuttonitemAlloc]initwithcustomview:label];//Create button array    Nsarray* ARRAYBTN = [NsarrayARRAYWITHOBJECTS:RIGHTBTN,ITEM03,Nil];//Assign a value to the right button array     Self. Navigationitem. Rightbarbuttonitems=ARRAYBTN;} -(void) pressleft{NSLog(@the left button is pressed! ");} -(void) pressright{NSLog(@"right button is pressed!" ");} - (void) Didreceivememorywarning {[SuperDidreceivememorywarning];//Dispose of any resources, can be recreated.}/ * #pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before navigation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using    [Segue Destinationviewcontroller]. Pass the selected object to the new view Controller.} */@end

iOS development from getting started to mastering--Navigation controller Basics

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.