label_textField
实现一个功能,不是难事。写出高质量的代码却不事任何程序员都可以做到的事。(感觉好难)
高质量代码特点:可复用,可移植,精炼等。
自定义视图:系统标准UI之外,自己组合而出的新的视图。
iOS提供了很多UI组件,借助它们,我们可以做各种程序。
自定义视图步骤
根据需求不同,自定义视图继承的类也有所不同。一般自定义视图会继承UIView。以下事自定义视图的要点:
1.创建一个UIView子类
2.在类的初始化方法中添加子视图
3.类的.h文件提供一些接口(方法),便于外界操作子视图
比如说创建一个label_TextFieldView继承于UIView
label_TextFieldView.h
@interface label_TextFieldView : UIView
// 把Label 和 TextField 设置为自定义视图的属性
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UITextField *textField;
@end
label_TextFieldView.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
// 设置左边的label
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width * 1 / 3, self.frame.size.height)];
// 设置背景颜色
self.label.backgroundColor = [UIColor lightGrayColor];
// 设置居中
self.label.textAlignment = NSTextAlignmentCenter;
// 设置字体颜色
self.label.textColor = [UIColor whiteColor];
[self addSubview:self.label];
[self.label release];
// 设置右边的textField
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(self.frame.size.width * 1 / 3, 0, self.frame.size.width * 2 / 3, self.frame.size.height)];
self.textField.borderStyle = UITextBorderStyleRoundedRect;
[self addSubview:self.textField];
[self.textField release];
}
return self;
}
// 属性内存管理
- (void)dealloc
{
[_label release];
[_textField release];
[super dealloc];
}
用自定义视图就可以优化一下之前那个登录界面了
用自定义视图替换Label和TextField
Label_TextFieldView *viewA = [[Label_TextFieldView alloc] initWithFrame:CGRectMake(20, 120, 280, 40)];
viewA.label.text = @"用户名";
[self.view addSubview:viewA];
viewA.textField.tag = 10000;
[viewA release];
iOS 程序启动流程
任何一个程序,无论事基于Mac OS还事iOS,程序都事从main.m文件的main函数开始执行的
main函数:
int main(int argc, char * argv[])
{
@autoreleasepool {
// 1.程序的启动都是从Main.m中的main函数开始的
// UIApplicationMain函数
// 作用:
// 1.建立一个事件循环 保证程序的持续执行
// 2.建立一个应用程序的对象(UIApplication类的对象)
// 3.给应用程序对象 指定一个 代理人(响应应用程序的各个状态 方便开发者做出不同的操作)
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
应用程序代理
应用程序代理,主要检测应用程序的状态并做出相应的处理
应用程序的状态有很多,比如:程序启动 进入活跃状态 进到后台 内存警告 收到远程消息等等
任何接受了UIApplicationDelegate协议的对象都可以成为应用程序代理
一旦应用程序的某种状态触发,就会执行相应的代理方法
UIApplicationDelegate
@implementation AppDelegate
// 当应用程序结束加载时调用
// 作用: 创建window 进行基本的设置
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 检测程序执行的方法
NSLog(@"%s", __FUNCTION__);
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// 创建一个 自定义视图 的对象
label_TextFieldView *view = [[label_TextFieldView alloc] initWithFrame:CGRectMake(20, 120, 280, 40)];
// 给view附值
view.label.text = @"丄杰";
[self.window addSubview:view];
[view release];
[_window release];
return YES;
}
// 属性内存管理
- (void)dealloc
{
NSLog(@"%s", __FUNCTION__);
[_window release];
[super dealloc];
}
// 当应用程序将要取消自己的 激活状态
// 作用: 告诉系统 应用程序将要进入后台
// 方便开发者暂停当前的任务 或者 游戏进度
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(@"%s", __FUNCTION__);
// 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.
}
// 当app进入后台时调用
// 在这个方法里 写一些保存数据的代码(数据库 写入本地)
// 为了防止程序 在后台时意外退出 丢失数据
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"%s", __FUNCTION__);
// 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.
}
// 当app 进入前台时调用
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"%s", __FUNCTION__);
// 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(@"%s", __FUNCTION__);
// 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(@"%s", __FUNCTION__);
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end