创建两个视图控制类MainViewController和SecondViewController

AppDelegate.h

#import<UIKit/UIKit.h>@interfaceAppDelegate:UIResponder<UIApplicationDelegate>@property(strong,nonatomic)UIWindow*window;@end

AppDelegate.m

#import"AppDelegate.h"#import"MainViewController.h"@implementationAppDelegate-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];//Overridepointforcustomizationafterapplicationlaunch.self.window.backgroundColor=[UIColorwhiteColor];[self.windowmakeKeyAndVisible];//视图控制器(UIViewController)//抽象类:不能直接使用,需要创建一个子类使用//创建一个ViewController控制器MainViewController*mainVC=[[MainViewControlleralloc]init];//在程序启动的时候,需要指定一个根视图控制器self.window.rootViewController=mainVC;[mainVCrelease];[_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

MainViewController.h

#import<UIKit/UIKit.h>@interfaceMainViewController:UIViewController@end

MainViewController.m

#import"MainViewController.h"#import"SecondViewController.h"@interfaceMainViewController()@end@implementationMainViewController-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];if(self){//CustominitializationNSLog(@"初始化");//视图控制器的指定初始化方法。。。。。//写在这里的代码一定会在初始化的时候执行//一般来说,在这个方法中,主要做数据的初始化。//比如数组/字典等容器的初始化}returnself;}-(void)loadView{[superloadView];NSLog(@"加载视图");}-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview.NSLog(@"视图加载完毕");//给视图控制器自带的view设置颜色self.view.backgroundColor=[UIColorwhiteColor];self.view.alpha=0.5;//button按钮UIButton*button=[UIButtonbuttonWithType:UIButtonTypeSystem];button.frame=CGRectMake(60,50,100,30);[buttonsetTitle:@""forState:UIControlStateNormal];button.backgroundColor=[UIColorblueColor];[buttonsetShowsTouchWhenHighlighted:YES];button.layer.cornerRadius=50;[buttonsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];[buttonaddTarget:selfaction:NULLforControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:button];//button1UIButton*button1=[UIButtonbuttonWithType:UIButtonTypeSystem];button1.frame=CGRectMake(150,50,100,30);[button1setTitle:@""forState:UIControlStateNormal];[button1setShowsTouchWhenHighlighted:YES];[button1setTitleColor:[UIColorblackColor]forState:UIControlStateNormal];button1.backgroundColor=[UIColoryellowColor];button1.layer.cornerRadius=50;[button1addTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];//这个方法在视图控制器自己的view已经被加载完毕的时候调用//在这个方法中,主要进行视图的铺设。}-(void)buttonClicked:(UIButton*)button{//初始化SecondViewController*secondVC=[[SecondViewControlleralloc]init];//参数1.要跳转的viewController//参数2.跳转的时候需不需要动画效果//参数3.block//改变跳转动画[secondVCsetModalTransitionStyle:2];[selfpresentViewController:secondVCanimated:YEScompletion:^{//block中填写,完成跳转之后,要执行的代码}];[secondVCrelease];}-(void)viewWillAppear:(BOOL)animated{[superviewDidAppear:animated];NSLog(@"视图将要出现");}//Calledwhentheviewisabouttomadevisible.Defaultdoesnothing-(void)viewDidAppear:(BOOL)animated{[superviewDidAppear:animated];NSLog(@"视图以已经出现");}//Calledwhentheviewhasbeenfullytransitionedontothescreen.Defaultdoesnothing-(void)viewWillDisappear:(BOOL)animated{[superviewDidAppear:animated];NSLog(@"视图将要消失");}//Calledwhentheviewisdismissed,coveredorotherwisehidden.Defaultdoesnothing-(void)viewDidDisappear:(BOOL)animated{[superviewDidAppear:animated];NSLog(@"视图已经消失");}//Calledaftertheviewwasdismissed,coveredorotherwisehidden.Defaultdoesnothing-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.//当系统收到内存警告的时候,调用NSLog(@"内存警告");}/*#pragmamark-Navigation//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation-(void)prepareForSegue:(UIStoryboardSegue*)seguesender:(id)sender{//Getthenewviewcontrollerusing[seguedestinationViewController].//Passtheselectedobjecttothenewviewcontroller.}*/@end


SecondViewController.h

#import<UIKit/UIKit.h>@interfaceSecondViewController:UIViewController@end

SecondViewController.m

#import"SecondViewController.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=[UIColormagentaColor];self.view.alpha=0.2;//button按钮UIButton*button=[UIButtonbuttonWithType:UIButtonTypeSystem];button.frame=CGRectMake(60,100,100,30);[buttonsetTitle:@""forState:UIControlStateNormal];button.backgroundColor=[UIColorblueColor];[buttonsetShowsTouchWhenHighlighted:YES];button.layer.cornerRadius=50;[buttonsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];[buttonaddTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:button];//button1UIButton*button1=[UIButtonbuttonWithType:UIButtonTypeSystem];button1.frame=CGRectMake(150,100,100,30);[button1setTitle:@""forState:UIControlStateNormal];button1.backgroundColor=[UIColoryellowColor];[button1setShowsTouchWhenHighlighted:YES];button1.layer.cornerRadius=50;[button1setTitleColor:[UIColorblackColor]forState:UIControlStateNormal];[button1addTarget:selfaction:NULLforControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:button1];}-(void)buttonClicked:(UIButton*)button{//点击返回[selfdismissViewControllerAnimated:YEScompletion:^{}];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}/*#pragmamark-Navigation//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation-(void)prepareForSegue:(UIStoryboardSegue*)seguesender:(id)sender{//Getthenewviewcontrollerusing[seguedestinationViewController].//Passtheselectedobjecttothenewviewcontroller.}*/@end