UI01程序的生命周期

来源:互联网 发布:淘宝女款皮衣冬 编辑:程序博客网 时间:2024/05/17 23:04

什么是UIApplication?

  • UIApplication对象是应用程序的象征

  • 每一个应用都有自己的UIApplication对象,而且是单例的

  • 通过[UIApplicationsharedApplication]可以获得这个单例对象

  • 一个iOS程序启动后创建的第一个对象就是UIApplication对象

  • 利用UIApplication对象,能进行一些应用级别的操作
UIApplication和delegate

   所有的移动操作系统都有个致命的缺点:app很容易受到打扰。比如一个来电或者锁屏会导致app进入后台甚至被终止
    还有很多其它类似的情况会导致app受到干扰,在app受到干扰时,会产生一些系统事件,这时UIApplication会通知它的delegate对象,让delegate代理来处理这些系统事件

delegate可处理的事件包括:
  • 应用程序的生命周期事件(如程序启动和关闭)

  • 系统事件(如来电)

  • 内存警告

iOS程序的启动过程


手动创建UIWindow的步骤
(详见http://blog.csdn.net/hierarch_lee/article/details/47324969)

AppDelegate.m文件中
 //1.创建window窗口    //    CGRect mainRect = [UIScreen mainScreen].bounds;        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    self.window.backgroundColor = [UIColor whiteColor];        //设置根视图控制器    RootViewController *rootVC = [[RootViewController alloc] init];//    rootVC.view.backgroundColor = [UIColor redColor];    self.window.rootViewController = rootVC;        [self.window makeKeyAndVisible];
RootViewController.m文件中
#import "RootViewController.h"@interface RootViewController ()@property (nonatomic, strong)UILabel *textLabel;@property (nonatomic, strong)UITextField *textField;- (void)initUserInterface;@end@implementation RootViewController- (void)viewDidLoad {    [super viewDidLoad];    [self initUserInterface];}- (void)buttonPressed:(id)sender{    //打印按钮上的文字    UIButton *button = sender;    NSLog(@"%@",button.currentTitle);            self.textLabel.text = self.textField.text;   //self.textField.text = self.textLabel.text ;}- (void)initUserInterface{    self.view.backgroundColor = [UIColor purpleColor];    /**< 文本标签 */    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(150, 100, 100, 40)];    //设置文本文字    label.text = @"Hello World!";    //设置文本背景色    label.backgroundColor = [UIColor greenColor];    //居中    label.textAlignment = NSTextAlignmentCenter;    //设置字体颜色    label.textColor = [UIColor redColor];    //设置字体大小    label.font = [UIFont systemFontOfSize:15];    self.textLabel = label;    //在视图上显示标签    [self.view addSubview:label];        /**< 文本输入框 */    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 40)];    //textField.text = @"HAHA";    //设置显示内容    textField.placeholder = @"请输入你的内容";    //设置文本框风格    textField.borderStyle = UITextBorderStyleRoundedRect;    self.textField = textField;    //在视图上显示文本输入框    [self.view addSubview:textField];        /**< 按钮 */    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];    //居中    button.center = CGPointMake(200, 300);    //大小    button.bounds = CGRectMake(0, 0, 100, 30);    //加文字    [button setTitle:@"确定" forState:UIControlStateNormal];    //设置文字颜色    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];    //背景颜色    button.backgroundColor = [UIColor greenColor];    //设置按钮的动作    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button];}@end



0 0
原创粉丝点击