1. 先直接贴出我们的appdelegate.m中的为实现的方法和执行结果.
#import "BIDAppDelegate.h"
@implementation BIDAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(@"%@",NSStringFromSelector(_cmd));
// 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.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"%@",NSStringFromSelector(_cmd));
// 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.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"%@",NSStringFromSelector(_cmd));
// 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.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(@"%@",NSStringFromSelector(_cmd));
// 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
{
NSLog(@"%@",NSStringFromSelector(_cmd));
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
2013-11-26 20:41:51.821 第十五章:后台处理:StateLab[2979:a0b] Cannot find executable for CFBundle 0x89ba120 </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk/System/Library/AccessibilityBundles/CertUIFramework.axbundle> (not loaded)
2013-11-26 20:41:52.236 第十五章:后台处理:StateLab[2979:a0b] applicationDidBecomeActive:
以上是app启动之后出现的代码.
2013-11-26 20:42:19.510 第十五章:后台处理:StateLab[2979:a0b] applicationWillResignActive:
2013-11-26 20:42:19.641 第十五章:后台处理:StateLab[2979:a0b] applicationDidEnterBackground:
以上是我们按了home之后出现的代码.
不论如何,
- (void)applicationWillTerminate:(UIApplication *)application
很少执行过.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// UIApplicationDidFinishLaunchingNotification;
- (void)applicationWillResignActive:(UIApplication *)application
{
//UIApplicationWillResignActiveNotification;
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplicationDidEnterBackgroundNotification
- (void)applicationWillEnterForeground:(UIApplication *)application
{
UIApplicationWillEnterForegroundNotification
- (void)applicationDidBecomeActive:(UIApplication *)application
{
UIApplicationDidBecomeActiveNotification
- (void)applicationWillTerminate:(UIApplication *)application
{
UIApplicationWillTerminateNotification
我们可以在适当的位置发布通知,然后在适当的位置接收通知.
3.下面看一个相互嵌套的 block语句
- (void)rotateLabelDown {
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
label.transform = CGAffineTransformMakeRotation(M_PI);
}
completion:^(BOOL finished){
[self rotateLabelUp];
}];
}
- (void)rotateLabelUp {
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
label.transform = CGAffineTransformMakeRotation(0);
}
completion:^(BOOL finished){
if (animate) {
[self rotateLabelDown];
}
}];
}
4.看看在didLoadView中发布通知吧,很简单.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
object:[UIApplication sharedApplication]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:[UIApplication sharedApplication]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillResignActive)
name:UIApplicationWillResignActiveNotification
object:[UIApplication sharedApplication]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];
CGRect bounds = self.view.bounds;
CGRect labelFrame = CGRectMake(bounds.origin.x, CGRectGetMidY(bounds) - 50,
bounds.size.width, 100);
self.label = [[UILabel alloc] initWithFrame:labelFrame];
label.font = [UIFont fontWithName:@"Helvetica" size:70];
label.text = @"Bazinga!";
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
// smiley.png is 84 x 84
CGRect smileyFrame = CGRectMake(CGRectGetMidX(bounds) - 42,
CGRectGetMidY(bounds)/2 - 42,
84, 84);
self.smileyView = [[UIImageView alloc] initWithFrame:smileyFrame];
self.smileyView.contentMode = UIViewContentModeCenter;
NSString *smileyPath = [[NSBundle mainBundle] pathForResource:@"smiley"
ofType:@"png"];
self.smiley = [UIImage imageWithContentsOfFile:smileyPath];
self.smileyView.image = self.smiley;
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
@"One", @"Two", @"Three", @"Four", nil]] ;
self.segmentedControl.frame = CGRectMake(bounds.origin.x + 20,
CGRectGetMaxY(bounds) - 50,
bounds.size.width - 40, 30);
[self.view addSubview:segmentedControl];
[self.view addSubview:smileyView];
[self.view addSubview:label];
NSNumber *indexNumber;
if ((indexNumber = [[NSUserDefaults standardUserDefaults]
objectForKey:@"selectedIndex"])) {
NSInteger selectedIndex = [indexNumber intValue];
self.segmentedControl.selectedSegmentIndex = selectedIndex;
}
// [self rotateLabelDown];
}
以下是调用SEL的方法实现.
- (void)applicationWillResignActive {
NSLog(@"VC: %@", NSStringFromSelector(_cmd));
animate = NO;
}
- (void)applicationDidBecomeActive {
NSLog(@"VC: %@", NSStringFromSelector(_cmd));
animate = YES;
[self rotateLabelDown];
}
- (void)applicationDidEnterBackground {
NSLog(@"VC: %@", NSStringFromSelector(_cmd));
UIApplication *app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier taskId;
taskId = [app beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"Background task ran out of time and was terminated.");
[app endBackgroundTask:taskId];
}];
if (taskId == UIBackgroundTaskInvalid) {
NSLog(@"Failed to start background task!");
return;
}
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"Starting background task with %f seconds remaining",
app.backgroundTimeRemaining);
self.smiley = nil;
self.smileyView.image = nil;
NSInteger selectedIndex = self.segmentedControl.selectedSegmentIndex;
[[NSUserDefaults standardUserDefaults] setInteger:selectedIndex
forKey:@"selectedIndex"];
// simulate a lengthy (25 seconds) procedure
[NSThread sleepForTimeInterval:25];
NSLog(@"Finishing background task with %f seconds remaining",
app.backgroundTimeRemaining);
[app endBackgroundTask:taskId];
});
}
- (void)applicationWillEnterForeground {
NSLog(@"VC: %@", NSStringFromSelector(_cmd));
NSString *smileyPath = [[NSBundle mainBundle] pathForResource:@"smiley"
ofType:@"png"];
self.smiley = [UIImage imageWithContentsOfFile:smileyPath];
self.smileyView.image = self.smiley;
}