UI程序的一般执行顺序:
先进入main里面,执行函数UIApplicationMain(),通过该函数创建应用程序对象和指定其代理并实现监听,当执行函数UIApplicationMain()时还会做一次跳转,跳转至AppDelegate
UIApplicationMain() 函数的三大功能:
1.创建应用的UIApplication对象
2.指定应用程序的代理对象,代理的主要作用:监听应用程序是如何运行的.
3.建立事件循环(runloop:这个循环是一个死循环).作用:一旦用户操作应用程序,应用程序要立即做出响应,但用户操作应用的时间不确定,所以必须让代理一直处于监听状态,直到程序退出为止.
代码如下:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
而AppDelegate其实也是一个代理,它遵循UIApplicationDelegate协议,这就是为何当一开始AppDelegate里会有很多方法的原因.(重写UIApplicationDelegate协议里面必须实现的方法)
下面简要介绍这些方法:
1.告诉代理程序即将启动完成,程序准备运行
代理实现这个方法的时候,第一步创建一个window对象,第二步给window配置属性,第三步将window设置为主屏幕并可见,如果window上有其他视图,通过window呈现即可
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
lf.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
2.告诉代理程序将要进入非活跃状态(暂停,将timer时间类停止)
- (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.
}
3.告诉代理应用程序已经进入后台(存储有用数据,释放一些内存等)
- (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.
}
4.告诉代理程序将要进入前台(取消暂停)
- (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.
}
5.告诉代理程序已经变得活跃(取消暂停状态的任务)
- (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.
}
6.
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
当系统提供的视图不满足开发实际要求的时候,这个时候就需要开发人员自定义视图.
常见的自定义视图是UILabel和UITextFiled一起组合起来
下面就以最简单的UILabel和UITextFiled组合的自定义视图为例说明:
首先新建一个类LTVIew
在LTVIew.h文件中,首先定义俩个属性UILabel和UITextField.
LTVIew.h
@interface LTVIew : UIView
@property (nonatomic,retain)UILabel *label;
@property (nonatomic,retain)UITextField *textField;
@end
然后在LTVIew.h中重写initWithFrame:方法,实现对象一初始化就有UILabel和UITextField.
LTVIew.m
#import "LTVIew.h"
@implementation LTVIew
//重写UIView的方法
-(id)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
[self p_setUp];
}
return self;
}
//写完这个方法,要立即在重写父类的初始化方法里调用
-(void)p_setUp{
_label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width/3, self.frame.size.height)];
_label.backgroundColor=[UIColor yellowColor];
[self addSubview:_label];
[_label release];
_textField=[[UITextField alloc]initWithFrame:CGRectMake(_label.frame.size.width, 0, self.frame.size.width*2/3, self.frame.size.height)];
_textField.backgroundColor=[UIColor redColor];
[self addSubview:_textField];
[_textField release];
}
-(void)dealloc{
[_textField release];
[_label release];
[super dealloc];
}
@end
然后就可以在AppDelegate.m文件中创建自定义对象并对其赋值属性使用额.
版权声明:本文为博主原创文章,未经博主允许不得转载。