视图控制器的生命周期

#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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值