iOS 基本启动流程
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
在以上的 main 函数中将功能都跳转到 UIApplicationMain 中, 而 UIApplicationMain 函数总共实现以下三步:
- 创建应用程序对象(UIApplication 对象)
- 创建应用程序代理对象, 指定应用程序代理, 应用程序代理用来检测应用程序的执行状态.
- 创建事件循环(runloop, 死循环), 监测用户对屏幕的操作, 一旦发现用户操作程序, 应用程序必须立即作出回应. 由于用户操作应用程序的时间未知, 所以需要一直进行监听, 直到应用程序退出.(在 AppDelegate中进行监听)
AppDelegate中函数说明 - 在 AppDelegate 中定义视图控制器 ViewController , 并设置为根视图控制器, 并在 ViewController 中对视图进行布局以及操作等.
ViewController 的说明
基础控件
UILabael
- UILabel 是UIView 的子类
- UILabel 的使用步骤:
//创建UILabel控件
UILabel *subLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
//配置属性
subLabel.text = @"subLabel";//设置显示的文字
//添加到父视图
[container addSubview:subLabel];
//释放
[subLabel release];
3 . UILabel 的属性
- tag – Label 的标签, 不同的对象不能有相同的 tag 值, 用户定义时使用100以上的值.
- backgroundColor – Label 的背景颜色(UIColor)
- text – Label 的文本内容(NSString)
- textColor – Label 的文本颜色(UIColor)
- textAlignment – Label 的文本对齐方式(NSTextAlignment)
typedef NS_ENUM(NSInteger, NSTextAlignment) {
NSTextAlignmentLeft = 0, // Visually left aligned
#if TARGET_OS_IPHONE
NSTextAlignmentCenter = 1, // Visually centered
NSTextAlignmentRight = 2, // Visually right aligned
#else /* !TARGET_OS_IPHONE */
NSTextAlignmentRight = 1, // Visually right aligned
NSTextAlignmentCenter = 2, // Visually centered
#endif
NSTextAlignmentJustified = 3, // Fully-justified. The last line in a paragraph is natural-aligned.
NSTextAlignmentNatural = 4, // Indicates the default alignment for script
} NS_ENUM_AVAILABLE_IOS(6_0);
- font – Label 的字体类型及大小(UIFont)
- numberOfLines – Label 显示文本的行数(NSInteger)
- lineBreakMode – Label 的行的截取方式(NSLineBreakMode)
- shadowColor – 设置 Label 的文本阴影颜色(UIColor)
- shadowOffset – 设置 Label 的文本阴影偏移量, 可以让文字更有立体感(CGSize), 关于阴影问题可以参考CALayer中的 shadow
UITextField
- UITextField 是 UIControl 的子类(UIControl 是 UIView 的子类)
- UITextField 使用步骤:
//创建 UITextField 控件
UITextField *subTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 25)];
//配置属性
subTextField.tag = 300;
//添加到父视图
[containerView addSubview:subTextField];
//释放
[subTextField release];
3.. UITextField 的常用属性
- backgroundColor
- tag
- text
- textColor
- textAlignment
- font
- clearOnBeginEditing – 当输入框开始编辑时, 清除输入框中的文字(BOOL)
- keyboardType – 设置键盘类型(UIKeyboardType)
- returnkeyType – 设置 return 按钮的样式(UIReturnKeyType)
- secureTextEntry – 设置输入框以密文形式显示(BOOL), 默认为 NO
- borderStyle – 设置输入框的样式(UITextBorderStyle)
- clearButtonMode – 设置输入框中清除按钮的模式(UITextFieldViewMode)
UIButton
- UIButton 是UIControl 的子类
- UIButton 使用步骤:
//创建UIButton 控件
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//配置属性
button.tag = 200;
//添加到父视图
[container addSubview:button];
3 . UIButton 的常用属性
- tag
- backgroundColor
- 设置 Button 上的文本
[button setTitle:@"确定" forState:UIControlStateNormal];//设置文本内容
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];//设置文本颜色
- 设置图片
[button setImage:[UIImage imageNamed:@"login_btn_press"] forState:UIControlStateNormal];
- 添加 Button 响应事件
/**
* target : 指定事件的响应对象
* action : 指定响应对象要去调用的方法, 处理 Button 的点击事件
* controlEvents : 事件的触发时机
* 注意 : handleButtonACtionL: 方法的参数可有可无, 如果有只能有一个, 因为参数为调用 addTarget:action:forControlEvents: 方法的对象
*/
[button addTarget:self action:@selector(handleButtonAction:) forControlEvents:UIControlEventTouchUpInside];