视图控制器的生命周期

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController



//使用IB创建视图时,Controller 的 view 需要使用此方法初始化

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

    if (self) {


    }

    

    NSLog(@"initWithNibName: bundle:");

    

    return self;

    

}


//视图加载之后被调用,视图控制器生命周期中只调用一次

- (void)viewDidLoad

{

    [superviewDidLoad];

    

    self.view.backgroundColor = [UIColorredColor];

    

    NSLog(@"viewDidLoad");


}


//视图将要出现的时候调用

-(void)viewWillAppear:(BOOL)animated

{

    NSLog(@"viewWillAppear:");

}


//视图出现之后调用

-(void)viewDidAppear:(BOOL)animated

{

    NSLog(@"viewDidAppear:");

}


//视图将要消失的时候调用

-(void)viewWillDisappear:(BOOL)animated

{

    NSLog(@"viewWillDisappear");

}


//视图消失的时候调用

-(void)viewDidDisappear:(BOOL)animated

{

    NSLog(@"viewDidDisappear:");

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];


}


@end



2013-11-23 18:23:13.134视图控制器的生命周期[536:c07] initWithNibName: bundle:

2013-11-23 18:23:13.148视图控制器的生命周期[536:c07] viewDidLoad

2013-11-23 18:23:13.148视图控制器的生命周期[536:c07] viewWillAppear:

2013-11-23 18:23:13.158视图控制器的生命周期[536:c07] viewDidAppear:


输出可以看出视图控制器的调用顺序,因为当前页面并没有让它消失,所以没有调用 viewWillDisappear, viewDidDisappear 方法 



AppDelegate的常用方法

#import "AppDelegate.h"

#import "ViewController.h"


@implementation AppDelegate


- (void)dealloc

{

    [_window release];

    [super dealloc];

}


//程序加载完成后调用,通常在此方法设置窗口和视图的初始化

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]]autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColorwhiteColor];

    

    ViewController *VC = [[ViewControlleralloc] init];

    

    UINavigationController *nav = [[UINavigationControlleralloc] initWithRootViewController:VC];

    

    self.window.rootViewController = nav;

    

    [self.windowmakeKeyAndVisible];

    return YES;

}


//程序挂起(锁屏或接收到短信或电话等时调用,停止定时器),在应用程序要由活动状态切换到非活动状态的时候,要执行的委托调用,如按home按钮,返回主屏幕,或全屏之间切换应用程序等;

- (void)applicationWillResignActive:(UIApplication *)application

{

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


//程序退到后台时调用(响应Home键)

- (void)applicationDidEnterBackground:(UIApplication *)application

{

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


//在应用程序将要进入前台时(激活),要执行的委托调用,刚好与applicationWillResignActive方法对应

- (void)applicationWillEnterForeground:(UIApplication *)application

{

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


//程序恢复(解锁或忽略短信或电话时调用恢复定时器)在应用程序已被激活后,要执行的委托调用,刚好与applicationDidEnterBackground方法对应

- (void)applicationDidBecomeActive:(UIApplication *)application

{

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


//程序中止时调用(响应短信或电话时调用,保存数据及退出程序前的清理工作)

- (void)applicationWillTerminate:(UIApplication *)application

{

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}


@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC的生命周期可以理解为请求从进入到离开的整个过程。在Spring MVC中,主要有以下几个生命周期阶段: 1. DispatcherServlet初始化:当应用启动时,DispatcherServlet将会被初始化,它是整个Spring MVC框架的前端控制器。 2. HandlerMapping查找处理器:DispatcherServlet接收到请求后,会通过HandlerMapping来查找对应的处理器(Controller)。 3. HandlerAdapter执行处理器:找到处理器后,DispatcherServlet会将请求交给HandlerAdapter来执行处理器方法,并获取处理结果。 4. HandlerInterceptor拦截处理器:在处理器执行之前或之后,可以通过HandlerInterceptor来进行一些预处理或后处理的操作。 5. HandlerExceptionResolver处理异常:如果在处理器的执行过程中出现异常,会通过HandlerExceptionResolver来处理异常,并返回给客户端相应的错误信息。 6. ViewResolver解析视图:处理器方法执行成功后,会返回一个逻辑视图名,ViewResolver会将逻辑视图名解析为具体的视图对象。 7. View渲染视图:解析到视图对象后,View会负责将模型数据填充到视图模板中,并最终生成响应内容。 8. HandlerExecutionChain执行链:在整个处理过程中,通过HandlerExecutionChain可以插入一些处理器拦截器、异常处理器等来对请求进行处理。 总结起来,Spring MVC的生命周期包括了DispatcherServlet初始化、HandlerMapping查找处理器、HandlerAdapter执行处理器、HandlerInterceptor拦截处理器、HandlerExceptionResolver处理异常、ViewResolver解析视图、View渲染视图以及HandlerExecutionChain执行链等多个阶段。通过这些阶段的协作,实现了请求的处理和响应的生成。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [SpringMVC 生命周期](https://blog.csdn.net/lvhaoguang0/article/details/84570827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [springMVC的生命周期详解](https://download.csdn.net/download/weixin_38520437/12784264)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值