iOS架构

一个程序是从main函数中开始运行,iOS程序也是一样。当我们创建一个iOS工程后,会看到这样的结构:

可以看到main.m.

main.m:

#import <UIKit/UIKit.h>


#import "AZAppDelegate.h"


int main(int argc, char * argv[])

{

    @autoreleasepool {

        //在实际开发中,几乎是不会修改main函数的。

        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AZAppDelegate class]));

        

        //第一、二个参数 argc,argv[]就是main函数传参,ios程序一般main函数是不会传参的

        

        //第三个参数如果传nil的话,那么UIApplicationMain会创建一个UIApplication类的对象。如果不传nil,必须传入UIAoolication类的子类class。函数会创建一个UIApplication子类的对象。

        

        // UIApplication类的对象,会监听系统修改当前应用状态时发送的通知。比如系统将应用程序切换到前台,切换到前台,来电打断

        

        //第四个参数是一个代理类的classUIApplication会创建一个代理对象

        

        

        /*

            AZAppDelegate的对象会成为UIApplication对象的代理,当Appplication的对象监听到系统发送的通知,即检测到当前应用程序状态改变,会委托AZAppDelegate的对象调用相应的方法。

         

         */

    }

}



AppDelegate.m:

#pragma mark - 以下都是实现UIApplicationDeleGate协议的方法


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

{

    NSLog(@"这个程序即将被载入内存,调用这个方法");

    

    //创建一个窗口对象,iOS程序一般只有一个窗口。

    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;

}


- (void)applicationWillResignActive:(UIApplication *)application

{

    NSLog(@"当程序即将停止活动状态,调用这个方法");

    

    // 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(@"当程序已经进入后台,调用这个方法");

    // 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(@"当程序即将回到前台,调用这个方法");

    // 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(@"当程序恢复活动时,调用这个方法");

    // 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(@"当内存不足,程序要崩溃,会先调用这个方法,在这个方法里可以释放一些内存,来尽量挽救这次崩溃");

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

}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值