星空网 > 软件开发 > 操作系统

IOS基础学习

IOS基础学习-1: iOS Simple View的启动流程

创建工程

    选择|File|New|Project,在弹出菜单中选择Simple View Application,即可完成新项目的创建。t1

IOS基础学习images/loading.gif' data-original="http://images2015.cnblogs.com/blog/826309/201512/826309-20151201160453140-1862958921.png" width="244" height="178" border="0" />

创建好的工程中会包含以下文件:t2

IOS基础学习

  • AppDelegate.h,
  • AppDelegate.m

  • ViewController.h

  • ViewController.m

  • Main.stroyboard

  • info.plist
  • main.m等文件

启动流程

app程序启动过程如下:

  1. UIApplicationMain 函数调用创建一个 UIApplication 对象及程序代理对象(本例为 AppDelegate)
  2. UIApplication 对象扫描 Info.plist 文件,将其中 Mainstoryboard file base name 所指定的 Storyboard 文件装入(通常为:MainStoryboard.storyboard) 
  3. UIApplication 对象从程序代理对象中获取窗口对象UIWindow(或创建一个UIWindow 新实例并将其与程序代理对象相关联)
  4. 将Storyboard 文件中 initial view controller 属性所指定的UIViewController 实例化,并将它赋予为 UIWindow 的root view controller
  5. 向程序代理对象发送  application:didFinishLaunchingWithOptions: 消息,以便程序员做自己的初始化工作

 

main函数

#import <UIKit/UIKit.h> //UIKit是基于Cocoa Touch的框架,包含了UILable, UIButton等各个控件元素#import "AppDelegate.h" //申明了AppDelegate函数,用于完成App的初始化配置等int main(int argc, char * argv[]) {  @autoreleasepool {    NSLog(@"main");//调用AppDelegate
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 
  }}

    和C代码一样,main为入口函数。在main函数中,导入了<UIKit/UIKit.h>以及AppDelegate.h

<UIKit/UIKit.h>为iOS主要框架,包含了各个主要控件。

    main函数的参数与其他Linux的标准main函数一样,int argc表示参数数量,char * argv[]为后面的参数,每个参数之间采用空格分开。

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

    UIApplicationMain()方法根据我们提供的AppDelegate类名称来创建UIApplication的一个实例,且将这个
AppDelegate作为UIApplication的委托,一般我们可以通过类方法[UIApplication shareApplication]来获取对UIApplication的一个引用。

    在UIApplication接收到系统事件和生命周期事件时,会把相应的事件传递给UIApplicationDelegate进行处理,下表所列的生命周期函数大都是可选的。

AppDelegate

AppDelegate为整个应用的一个代理,提供程序启动,退出等类似监控的接口。

AppDelegate.h

#import <UIKit/UIKit.h>//需要实现UIApplicationDelegate的方法@interface AppDelegate : UIResponder <UIApplicationDelegate> //申明了UIWindow类,名称为window的属性,它是一个窗口,用来接收消息和加载控件@property (strong, nonatomic) UIWindow *window; @end

AppDelegate类中,继承了UIResponder类,并需要实现UISpplicationDelegate协议。

AppDelegate.m

在UIApplication接收到系统事件和生命周期事件时,会把相应的事件传递给UIApplicationDelegate进行处理,

#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  // Override point for customization after application launch.  NSLog(@"didFinishLaunchingWithOptions");  return YES;}- (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.  NSLog(@"applicationWillResignActive");}- (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.  NSLog(@"applicationDidEnterBackground");}- (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.  NSLog(@"applicationWillEnterForeground");}- (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.  NSLog(@"applicationDidBecomeActive");}- (void)applicationWillTerminate:(UIApplication *)application {  // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.  NSLog(@"applicationWillTerminate");}@end

具体见下面表格

函数说明
- (void)applicationWillResignActive:(UIApplication *)application当应用程序将要入非活动状态执行,在此期间,应用程序不接收消息或事件,比如来电话了
- (void)applicationDidBecomeActive:(UIApplication *)application当应用程序入活动状态执行,这个刚好跟上面那个方法相反
- (void)applicationDidEnterBackground:(UIApplication *)application当程序被推送到后台的时候调用。所以要设置后台继续运行,则在这个函数里面设置即可
- (void)applicationWillEnterForeground:(UIApplication *)application当程序从后台将要重新回到前台时候调用,这个刚好跟上面的那个方法相反。
- (void)applicationWillTerminate:(UIApplication *)application当程序将要退出是被调用,通常是用来保存数据和一些退出前的清理工作。这个需要要设置UIApplicationExitsOnSuspend的键值。
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)applicationiPhone设备只有有限的内存,如果为应用程序分配了太多内存操作系统会终止应用程序的运行,在终止前会执行这个方法,通常可以在这里进行内存清理工作防止程序被终止
- (void)applicationDidFinishLaunching:(UIApplication*)application当程序载入后执行
- (void)application:(UIApplication)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame当StatusBar框将要变化时执行

- (void)application:(UIApplication*)application willChangeStatusBarOrientation:
(UIInterfaceOrientation)newStatusBarOrientation
duration:(NSTimeInterval)duration

当StatusBar框方向将要变化时执行
- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url通过url执行

 

    iPhone中的应用程序很容易受到打扰,比如一个来电可能导致应用程序失去焦点,如果这个时候接听了电话,那么应用程序会转到后台运行。
还有很多其它类似的事件会导致iPhone应用程序失去焦点,在应用程序失去焦点前会调用委托类的applicationWillResignActive()方法,
而应用程序再次获取到焦点的时候会调用applicationDidBecomeActive()方法。 

    比如在运行应用程序的时候锁屏会调用委托类的applicationWillResignActive()方法,而当屏幕被解锁的时候,又会调用applicationDidBecomeActive()方法。 

    UIApplication委托AppDelegate,则AppDelegate必须得实现UIApplicationDelegate协议,这个协议我们可以当成是java中的一个接口,
协议中定义了一系列方法,我们必须在子类中将其实现,然后底层UIApplication会自动去调用我们已经定义好的方法,这有点类似java中Ioc方向控制机制。

AppDelegate的应用例子

页面的加载和配置

在didFinishLaunchingWithOptions方法中,主要完成页面初始化时的准备工作,例如:创建各种视图,读取并设置配置文件等。

读取和配置的例子:

NSUserDefaults *usrConfig= [NSUserDefaults standardUserDefaults];  //创建UserDefaults对象    //检查是否之前app运行过  if ([ usrConfig boolForKey:@"hasRunBefore"] != YES)  {    // 设置个标记使此if中字段只在第一次运行时执行    [ usrConfig setBool:YES forKey:@"hasRunBefore"];    [ usrConfig synchronize];// 把设置的键值对同步

创建窗口并设置背景

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  //初始化窗口self.window.backgroundColor = [UIColor whiteColor]; //设置背景色 [self.window makeKeyAndVisible];  //显示窗口

设置icon上的数字图标

//设置主界面icon上的数字图标,在2.0中引进, 缺省为0   [UIApplication sharedApplication].applicationIconBadgeNumber = 4;

设置摇动手势的时候,是否支持redo,undo操作

//摇动手势,是否支持redo undo操作。   //3.0以后引进,缺省YES   [UIApplication sharedApplication].applicationSupportsShakeToEdit =YES;

全局变量设置

可以在AppDelegate.h中定义需要全局使用的变量

@interface AppDelegate:UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) NSMutableArray*info;@property (strong, nonatomic) NSMutableArray *name;@end

在AppDelegate.m的didFinishLaunchingWithOptions方法中,初始化这些属性:

self.info = [NSMutableArray arrayWithObjects:@"",@"",@"",nil];self.name = [NSMutableArray arrayWithObjects:@"",@"",@"",nil];

在其他需要使用全局变量的方法中,获取全局变量

AppDelegate *appDelegate=[[UIApplication sharedApplication] delegate];

StoryBoard

UIApplication 对象扫描 Info.plist 文件,根据设定的storyboard名称,加载storyboard.

IOS基础学习

t2.5

Storyboard中,根据is Initial View Controller 和 Custom Class信息,实例化ViewController,再进入ViewController类的执行过程

IOS基础学习

Storyboard中设计的标签或者按钮通过IBOutlet和IBAction属性,定义在ViewController.h中,这样就可以通过代码,修改storyboard中的内容。

t2.6

IOS基础学习IOS基础学习

ViewController

ViewController.h中,完成storyboard中定义的输出口IBOutlit和按键时间IBAction的定义。

#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end

ViewController.m中,完成界面的最终配置和加载。

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {  [super viewDidLoad];  // Do any additional setup after loading the view, typically from a nib.   NSLog(@"viewDidLoad");}- (void)didReceiveMemoryWarning {  [super didReceiveMemoryWarning];  // Dispose of any resources that can be recreated.}@end

至此,就可以完成简单的SimpleViewApplication的加载和配置工作。

启动打样流程如下:

IOS基础学习

当系统进入后台后, WillResignActive和DidEnterBackground被调用。

IOS基础学习

《版权所有,转载请带有作者名称》




原标题:IOS基础学习

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流