UINavigationController页面切换合基本设置
新建的类和文件名
AppDelegate.h
#import<UIKit/UIKit.h>@interfaceAppDelegate:UIResponder<UIApplicationDelegate>@property(strong,nonatomic)UIWindow*window;@end
AppDelegate.m
#import"AppDelegate.h"#import"FirstViewController.h"#import"SecondViewController.h"#import"ThirdViewController.h"#import"ForthViewController.h"@implementationAppDelegate-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];//Overridepointforcustomizationafterapplicationlaunch.self.window.backgroundColor=[UIColorwhiteColor];[self.windowmakeKeyAndVisible];FirstViewController*firstVC=[[FirstViewControlleralloc]init];//导航视图控制器的使用(UINavigationController)//创建的时候,要给他指定一个默认显示的viewControllerUINavigationController*naviVC=[[UINavigationControlleralloc]initWithRootViewController:firstVC];//把navigationController指定为window的根视图控制器self.window.rootViewController=naviVC;[firstVCrelease];[_windowrelease];returnYES;}-(void)dealloc{[_windowrelease];[superdealloc];}-(void)applicationWillResignActive:(UIApplication*)application{//Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.//Usethismethodtopauseongoingtasks,disabletimers,andthrottledownOpenGLESframerates.Gamesshouldusethismethodtopausethegame.}-(void)applicationDidEnterBackground:(UIApplication*)application{//Usethismethodtoreleasesharedresources,saveuserdata,invalidatetimers,andstoreenoughapplicationstateinformationtorestoreyourapplicationtoitscurrentstateincaseitisterminatedlater.//Ifyourapplicationsupportsbackgroundexecution,thismethodiscalledinsteadofapplicationWillTerminate:whentheuserquits.}-(void)applicationWillEnterForeground:(UIApplication*)application{//Calledaspartofthetransitionfromthebackgroundtotheinactivestate;hereyoucanundomanyofthechangesmadeonenteringthebackground.}-(void)applicationDidBecomeActive:(UIApplication*)application{//Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.Iftheapplicationwaspreviouslyinthebackground,optionallyrefreshtheuserinterface.}-(void)applicationWillTerminate:(UIApplication*)application{//Calledwhentheapplicationisabouttoterminate.Savedataifappropriate.SeealsoapplicationDidEnterBackground:.}@end
FirstViewController.h
#import<UIKit/UIKit.h>@interfaceFirstViewController:UIViewController@end
FirstViewController.m
#import"FirstViewController.h"#import"SecondViewController.h"@interfaceFirstViewController()@end@implementationFirstViewController-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];if(self){//Custominitialization}returnself;}-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview.self.view.backgroundColor=[UIColorbrownColor];self.view.alpha=0.3;self.title=@"第一页面";//1.改变navigationBar的样式//一个导航控制器只有一个navigationBar//导航栏是否有一个模糊效果(默认为YES)[self.navigationController.navigationBarsetTranslucent:YES];//导航栏颜色//[self.navigationController.navigationBarsetBackgroundColor:[UIColorblueColor]];[self.navigationController.navigationBarsetBarTintColor:[UIColoryellowColor]];//隐藏导航栏[self.navigationControllersetNavigationBarHidden:NOanimated:YES];//navigationItem作用:每个视图控制器要显示在导航栏上的内容//当导航控制器推出一个新的vc的时候,会读取这个属性的所有内容显示到导航栏上self.navigationItem.title=@"First";//设置左右两边的按钮self.navigationItem.leftBarButtonItem=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemOrganizetarget:selfaction:@selector(buttonClicked:)];self.navigationItem.rightBarButtonItem=[[UIBarButtonItemalloc]initWithTitle:@"hehe"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(buttonClicked:)];//返回字体颜色[self.navigationController.navigationBarsetTintColor:[UIColorredColor]];UIButton*botton=[[UIButtonalloc]initWithFrame:CGRectMake(80,100,90,30)];botton.backgroundColor=[UIColorcyanColor];[bottonsetTitle:@"下页面"forState:UIControlStateNormal];[bottonsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];[bottonsetShowsTouchWhenHighlighted:YES];botton.layer.cornerRadius=5;[bottonaddTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:botton];}-(void)buttonClicked:(UIButton*)button{//利用navigetion推出新页面//1.创建一个新的viewControllerSecondViewController*secondVC=[[SecondViewControlleralloc]init];//2.把页面推出secondVC.name=button.currentTitle;//可以通过属性直接获取当前的viewController所在的导航控制器[self.navigationControllerpushViewController:secondVCanimated:YES];//3.内存管理[secondVCrelease];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}/*#pragmamark-Navigation//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation-(void)prepareForSegue:(UIStoryboardSegue*)seguesender:(id)sender{//Getthenewviewcontrollerusing[seguedestinationViewController].//Passtheselectedobjecttothenewviewcontroller.}*/@end
SecondViewController.h
#import<UIKit/UIKit.h>@interfaceSecondViewController:UIViewController@property(nonatomic,copy)NSString*name;@end
SecondViewController.m
#import"SecondViewController.h"#import"ThirdViewController.h"@interfaceSecondViewController()@end@implementationSecondViewController-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];if(self){//Custominitialization}returnself;}-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview.self.view.backgroundColor=[UIColorblueColor];//self.view.alpha=0.3;self.title=@"第二页面";UIButton*button=[[UIButtonalloc]initWithFrame:CGRectMake(110,100,90,30)];[buttonsetTitle:self.nameforState:UIControlStateNormal];[buttonsetTitleColor:[UIColoryellowColor]forState:UIControlStateNormal];button.backgroundColor=[UIColorredColor];button.layer.cornerRadius=5;[buttonaddTarget:selfaction:@selector(buttonClikded:)forControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:button];//UIImage*p_w_picpath=[UIImagep_w_picpathNamed:@"4.png"];self.navigationItem.rightBarButtonItem=[[[UIBarButtonItemalloc]initWithImage:p_w_picpathstyle:UIBarButtonItemStylePlaintarget:selfaction:NULL]autorelease];UISegmentedControl*segment=[[UISegmentedControlalloc]initWithItems:@[@"红茶",@"绿茶"]];self.navigationItem.titleView=segment;[segmentrelease];[self.navigationController.navigationBarsetTintColor:[UIColorblueColor]];}-(void)buttonClikded:(UIButton*)button{ThirdViewController*thirdVC=[[ThirdViewControlleralloc]init];[self.navigationControllerpushViewController:thirdVCanimated:YES];[thirdVCrelease];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}/*#pragmamark-Navigation//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation-(void)prepareForSegue:(UIStoryboardSegue*)seguesender:(id)sender{//Getthenewviewcontrollerusing[seguedestinationViewController].//Passtheselectedobjecttothenewviewcontroller.}*/@end
ThirdViewController.h
#import<UIKit/UIKit.h>@interfaceThirdViewController:UIViewController@end
ThirdViewController.m
#import"ThirdViewController.h"#import"ForthViewController.h"@interfaceThirdViewController()@end@implementationThirdViewController-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];if(self){//Custominitialization}returnself;}-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview.self.view.backgroundColor=[UIColormagentaColor];self.title=@"第三页";UIButton*button=[[UIButtonalloc]initWithFrame:CGRectMake(150,100,90,30)];[buttonsetTitle:@"下个页面"forState:UIControlStateNormal];button.backgroundColor=[UIColorcyanColor];button.layer.cornerRadius=5;[buttonaddTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:button];self.navigationItem.leftBarButtonItem=[[UIBarButtonItemalloc]initWithImage:[UIImagep_w_picpathNamed:@"3.png"]style:UIBarButtonItemStylePlaintarget:selfaction:NULL];}-(void)buttonClicked:(UIButton*)button{ForthViewController*forthVC=[[ForthViewControlleralloc]init];[self.navigationControllerpushViewController:forthVCanimated:YES];[forthVCrelease];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}/*#pragmamark-Navigation//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation-(void)prepareForSegue:(UIStoryboardSegue*)seguesender:(id)sender{//Getthenewviewcontrollerusing[seguedestinationViewController].//Passtheselectedobjecttothenewviewcontroller.}*/@end
第四个页面和第三个页面相同
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。