IOS AppDelegate.m 详解

今天在学习 IOS 中 Storyboard 的使用中,操作都是没问题的,就是启动后一直出现的是 白色 界面,不能进行跳转操作。找了好久也没找到方法,最后,问了老大,发现了问题所在,原来是 AppDelegate.m 文件中的, 该注释掉的没注释掉 :

 

复制代码
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //Override point for customization after application launch.
    //self.window.backgroundColor = [UIColor whiteColor];
    //[self.window makeKeyAndVisible];
    return YES;
}
复制代码
 
因为之前一直对 IOS 中的  AppDelegate.m 不太了解,才会导致这个问题,所以这次一定要彻底的搞明白  AppDelegate.m  这个重要的文件,这将极大的帮助你了解 Xcode 中 IOS 的开发~
 
关于 AppDelegate,有一个国外的用户,总结的特别好:
 
AppDelegate.h/m define a class that manages the application overall. The app will create one instance of that class and send that object messages that let the delegate influence the app's behavior at well-defined times. For example, -application:didFinishLaunchingWithOptions: is sent when the app has finished launching and is ready to do something interesting. Take a look at the  UIApplicationDelegate reference page for a list of messages that the app delegate can implement to modify the behavior of the application.
 
看到了吧,如果通过上面的解释,可以看出来,AppDelegate.h/m 是一个很重要的文件,如果你做过 Asp.Net 开发,那么这个文件就类似于 .Net 中的 Global.ascx 文件,就是做一些全局变量的控制的。
 
 
下面就过一下,AppDelegate.h/m 中默认的方法:
 
 
1. application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 
Tells the delegate when the application has launched and may have additional launch options to handle.
 
在应用程序启动后,要执行的委托调用。
 
 
 
2. applicationWillResignActive:(UIApplication *)application
 
Tells the delegate that the application is about to become inactive.This method is called to let your application know that it is about to move from the active to inactive state.After calling this method, the application also posts a UIApplicationWillResignActiveNotification notification to give interested objects a chance to respond to the transition.
 
在应用程序将要由活动状态切换到非活动状态时候,要执行的委托调用,如 按下 home 按钮,返回主屏幕,或全屏之间切换应用程序等。
 
 
 
3. applicationDidEnterBackground:(UIApplication *)application
 
Tells the delegate that the application is now in the background.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.
 
在应用程序已进入后台程序时,要执行的委托调用。
 
 
 
4. applicationWillEnterForeground:(UIApplication *)application
 
Tells the delegate that the application is about to enter the foreground.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.
 
在应用程序将要进入前台时(被激活),要执行的委托调用,刚好与 applicationWillResignActive 方法相对应。
 
 
 
5. applicationDidBecomeActive:(UIApplication *)application
 
Tells the delegate that the application has become active.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.
 
在应用程序已被激活后,要执行的委托调用,刚好与  applicationDidEnterBackground 方法相对应。
 
 
6. applicationWillTerminate:(UIApplication *)application
 
Tells the delegate when the application is about to terminate.Called when the application is about to terminate. Save data if appropriate.
 
在应用程序要完全推出的时候,要执行的委托调用。
 
 
有没有发现,IOS 中的 AppDelegate.m/h 文件是很重要的呢,因为它是对 Application 的整个生命周期进行管理的,有木有~



Cocoa Touch广泛使用委托(delegate),它是负责为其他对象处理特定任务的类。通过应用程序委托,我们能够在某些预定义时间内为UIApplication做一些工作。每个IOS应用都有且只有一个UIApplication实例,它负责应用程序的运行循环。


AppDelegate是类似于android的Application的作用,提供一些应用程序级别的状态的回调,还可以定义一些全局变量,具体的方法如下:

1- (void)applicationWillResignActive:(UIApplication *)application
//说明:当应用程序将要入非活动状态执行,在此期间,应用程序不接收消息或事件,比如来电话了
2- (void)applicationDidBecomeActive:(UIApplication *)application
//说明:当应用程序入活动状态执行,这个刚好跟上面那个方法相反
3- (void)applicationDidEnterBackground:(UIApplication *)application
//说明:当程序被推送到后台的时候调用。所以要设置后台继续运行,则在这个函数里面设置即可
4- (void)applicationWillEnterForeground:(UIApplication *)application
//说明:当程序从后台将要重新回到前台时候调用,这个刚好跟上面的那个方法相反。
5- (void)applicationWillTerminate:(UIApplication *)application
//说明:当程序将要退出是被调用,通常是用来保存数据和一些退出前的清理工作。这个需要要设置UIApplicationExitsOnSuspend的键值。
6- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
//说明:iPhone设备只有有限的内存,如果为应用程序分配了太多内存操作系统会终止应用程序的运行,在终止前会执行这个方法,通常可以在这里进行内存清理工作防止程序被终止
7- (void)applicationSignificantTimeChange:(UIApplication*)application
//说明:当系统时间发生改变时执行
8- (void)applicationDidFinishLaunching:(UIApplication*)application
//说明:当程序载入后执行
9- (void)application:(UIApplication)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame
//说明:当StatusBar框将要变化时执行
10- (void)application:(UIApplication*)application willChangeStatusBarOrientation:
(UIInterfaceOrientation)newStatusBarOrientation
duration:(NSTimeInterval)duration
//说明:当StatusBar框方向将要变化时执行
11- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url
//说明:当通过url执行
12- (void)application:(UIApplication*)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation
//说明:当StatusBar框方向变化完成后执行
13- (void)application:(UIApplication*)application didChangeSetStatusBarFrame:(CGRect)oldStatusBarFrame
//说明:当StatusBar框变化完成后执行

在AppDelegate.h 中定义全局变量,
注意是在构造函数里面声明:
@interface AppDelegate : UIResponder <UIApplicationDelegate>{
NSString *appLanguage;


实现
@property (strong,nonatomic) NSString *appLanguage;
@end


在AppDelegate.m 文件中 
@implementation AppDelegate
@synthesize appLanguage=appLanguage;

//注意释放
- (void)dealloc
{
[appLanguage release];



在需要使用的视图控制器中,应用appdelegate
AppDelegate *appDelegate=[[UIApplication sharedApplication] delegate];
appdelegate.appLanguage==@"english";


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值