iOS基础 - 完善键盘处理

1.完善键盘处理

步骤一:创建一个数组,里面装着所有的文本框。

步骤二:监听所有文本框的开始编辑,设置所有文本框的代理为控制器

1.设置生日和城市不允许键盘输入

2.当开始编辑的时候调用,用一个成员属性,记录住当前聚焦的文本框

步骤三:完成工具条的代理方法

步骤四:判断工具条上的按钮是否能点击

步骤五:将文本框数组按照y值排序

步骤六:监听系统发出键盘滚动通知

步骤七:当键盘挡住文本框将视图往上移

步骤八:移除监听通知

2.一切控件的封装最好继承UIView

3.项目中常见文件

1.main 程序的入口

2.pch 当项目中有些宏或者常用的东西放在pch,公司中打开一个项目先应该看的文件。

4.屏幕适配:视网膜会自动加载@2x

5.程序生命周期

 

 

 

 

一、UITextField的代理方法

#pragma mark 当文本框开始编辑的时候调用---开始聚焦

- (void)textFieldDidBeginEditing:(UITextField *)textField

 

二、排序

1.可变数组的排序(NSMutableArray

* sortUsingComparator:方法调完,会直接改变array这个可变数组内部对象的顺序

[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {}];

排序过程中会不断地调用block,传入两个需要比较的对象:id obj1, id obj2

* block必须有返回值:NSComparisonResult

* NSComparisonResult3种取值:

NSOrderedAscending = -1L, // 右边的对象排后面

NSOrderedSame, // 一样

NSOrderedDescending // 左边的对象排后面

2.不可变数组的排序(NSArray

* sortedArrayUsingComparator:方法并不会改变array数组内部的顺序

* sortedArrayUsingComparator:方法会返回一个新的已经排好序的数组sortedArray

NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {}];

 

三、监听键盘的显示和隐藏

1.监听键盘通知

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

// 1.显示键盘

[center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

// 2.隐藏键盘

[center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

2.移除键盘通知(非ARC必须写)

- (void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

3.注意点:当弹出一个新的键盘时,才会发出显示键盘的通知

 

四、UI控件常见属性总结

1.UIView

// 如果userInteractionEnabled=NO,不能跟用户交互

@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;

// 控件的标记(父控件通过标记可以找到对应的子控件)

@property(nonatomic) NSInteger tag;

// 控件的位置和尺寸(以父控件的左上角为坐标原点)

@property(nonatomic) CGRect            frame;

// 控件的位置和尺寸(以控件本身的左上角为坐标原点)

@property(nonatomic) CGRect            bounds;

// 控件的中点位置(以父控件的左上角为坐标原点)

@property(nonatomic) CGPoint           center;

// 形变属性:旋转、缩放、平移

@property(nonatomic) CGAffineTransform transform;

// 父控件

@property(nonatomic,readonly) UIView       *superview;

// 所有的子控件

@property(nonatomic,readonly,copy) NSArray *subviews;

2.UILabel

// 显示的文字

@property(nonatomic,copy)   NSString           *text;

// 字体

@property(nonatomic,retain) UIFont             *font;

// 文字颜色

@property(nonatomic,retain) UIColor            *textColor;

// 文字的排列方式(左对齐、居中、右对齐)

@property(nonatomic)        NSTextAlignment    textAlignment;

// 设置行数(行数==0代表自动换行)

@property(nonatomic) NSInteger numberOfLines;

3.UIImageView

// 显示的图片

@property(nonatomic,retain) UIImage *image;

// 设置序列帧图片数组(按顺序播放animationImages数组中的图片)

@property(nonatomic,copy) NSArray *animationImages;

// 序列帧动画的持续时间

@property(nonatomic) NSTimeInterval animationDuration;

// 序列帧动画的执行字数(默认是0,代表无限循环)

@property(nonatomic) NSInteger      animationRepeatCount;

4.UIScrollView

// 表示UIScrollView所滚动的位置

@property(nonatomic) CGPoint contentOffset;

// 表示UIScrollView的内容尺寸(能滚动的范围)

@property(nonatomic)         CGSize                       contentSize;

// 增加UIScrollView额外的边缘滚动区域

@property(nonatomic)         UIEdgeInsets                 contentInset;

// 代理

@property(nonatomic,assign) id<UIScrollViewDelegate>      delegate;

5.UITableView

6.UIPickerView

7.UIControl

// 是否可用

@property(nonatomic,getter=isEnabled) BOOL enabled;

// 自动拥有很多种状态

// 可以通过下面的方法来监听控件内部的一些事件:点击、值改变

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

1> UIDatePicker

// 设置模式(类型)

@property(nonatomic) UIDatePickerMode datePickerMode;   

// 设置区域(zh_CN代表天朝)

@property(nonatomic,retain) NSLocale      *locale;

// 设置当前时间

@property(nonatomic,retain) NSDate        *date;

// UIDatePicker内部显示的日期更改了,就会触发值改变事件

2> UISwitch

// 控制开关状态

@property(nonatomic,getter=isOn) BOOL on;

- (void)setOn:(BOOL)on animated:(BOOL)animated;

// UISwitch内部开关状态更改了,就会触发值改变事件

3> UISegmentControl

// 一共有多少块区域

@property(nonatomic,readonly) NSUInteger numberOfSegments;

// 当前选中区域的位置

@property(nonatomic) NSInteger selectedSegmentIndex;

// UISegmentControl内部选中的区域更改了,就会触发值改变事件

4> UISlider

// 设置当前的进度值

@property(nonatomic) float value;

// 设置最小的进度值

@property(nonatomic) float minimumValue;

// 设置最大的进度值

@property(nonatomic) float maximumValue;

// UISlider内部的进度值更改了,就会触发值改变事件

5> UIButton

// 快速创建一个按钮

+ (id)buttonWithType:(UIButtonType)buttonType;

// 设置按钮的内边距

@property(nonatomic) UIEdgeInsets contentEdgeInsets;

// 按钮内部的标签控件

@property(nonatomic,readonly,retain) UILabel     *titleLabel;

// 按钮内部的图片控件

@property(nonatomic,readonly,retain) UIImageView *imageView;

// 设置内部titleLabel显示的文字

- (void)setTitle:(NSString *)title forState:(UIControlState)state;

// 设置内部titleLabel的文字颜色

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;

// 设置内部imageView显示的图片

- (void)setImage:(UIImage *)image forState:(UIControlState)state;

// 设置背景图片

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;

- (NSString *)titleForState:(UIControlState)state;

- (UIColor *)titleColorForState:(UIControlState)state;

- (UIImage *)imageForState:(UIControlState)state;

- (UIImage *)backgroundImageForState:(UIControlState)state;

6> UITextField(通过delegate监听内部的事件)

8.UIAlertView

// 创建一个UIAlertView对话框

/*

 title : 对话框标题

 message : 对话框中间显示的文字内容

 cancelButtonTitle : 取消按钮的文字

 otherButtonTitles : 其他按钮的文字(设置多个)

 delegate : 用来监听alertView上面按钮的点击

 */

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;

// 显示

- (void)show;

 

五、项目常见文件

1.main.m

里面有一个程序的入口:main函数

2.Prefix.pch文件

* pch文件中的内容 能被 项目中的其他任何文件 共享\包含\访问

如果定义的内容只用在OC环境中,就必须定义在#ifdef __OBJC__#endif之间

3.发布程序的时候自动去除打印语句

#ifdef DEBUG

#define MyLog(...)  NSLog(__VA_ARGS__)

#else

#define MyLog(...)

#endif

4.InfoPlist.strings

app的本地化相关(多语言版本)

5.Info.plist

1> 全局配置文件

2> 新旧配置文件的对比

Xcode3的时候,全局配置文件名:Info.plist

Xcode4开始,全局配置文件名:项目名-Info.plist

3> 项目中自定义的plist文件中不要包含info这个字眼

4> 常见的配置

Bundle display name : 软件名称

Bundle identifier : app的唯一标识

Bundle versions string, short : 软件版本号(更新app

Main storyboard file base name : 设置程序一启动就加载的storyboard文件

 

六、屏幕适配

1.为非视网膜\视网膜屏幕分别准备2份图片,比如:

1> 非视网膜 abc.png

2> 视网膜 abc@2x.png

2.程序启动图片

程序启动过程中会自动全屏显示Default.png图片,程序启动完毕就会隐藏Default.png图片

* Default.png 非视网膜

* Default@2x.png 3.5英寸的视网膜

* Default-568h@2x.png 4英寸的视网膜

3.软件图标

系统会自动把Icon.png当做应用程序的软件图标

关于软件的图标规格,可以搜索官方文档:app icon

 

七、UIApplication

1.简介

1> 整个应用程序的象征,一个应用程序就一个UIApplication对象,使用了单例设计模式

2> 通过[UIApplication sharedApplication]访问这个单例对象

2.常见用法

1> 设置图标右上角的红色提示数字

app.applicationIconBadgeNumber = 10;

2> 设置状态栏的样式

app.statusBarStyle = UIStatusBarStyleBlackOpaque;

3> 控制状态栏的显示和隐藏

app.statusBarHidden = YES;

4> 显示状态栏上面的圈圈

app.networkActivityIndicatorVisible = YES;

5> 打开外部资源

打开网页

[app openURL:[NSURL URLWithString:@http://www.baidu.com]];

打电话

[app openURL:[NSURL URLWithString:@"tel://10086"]];

发短信

[app openURL:[NSURL URLWithString:@"sms://10086"]];

6> 代理属性(当应用程序发生了一些系统级别的事件,就会通知代理,交给代理去处理)

@property(nonatomic,assign) id<UIApplicationDelegate> delegate;

 

八、UIApplicationDelegate的代理方法

#pragma mark  程序加载完毕(启动完毕)就会调用一次

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

#pragma mark 应用程序失去焦点的时候调用(一个app如果失去焦点,就不能跟用户进行交互)

- (void)applicationWillResignActive:(UIApplication *)application

#pragma mark 程序进入后台就会调用

- (void)applicationDidEnterBackground:(UIApplication *)application

#pragma mark 程序即将进入前台的时候调用

- (void)applicationWillEnterForeground:(UIApplication *)application

#pragma mark 应用程序获得焦点的时候调用(一个app只有获得焦点之后才能跟用户进行交互)

- (void)applicationDidBecomeActive:(UIApplication *)application

#pragma mark 程序即将被关闭的时候可能会被调用

- (void)applicationWillTerminate:(UIApplication *)application

#pragma mark 程序接收到内存警告都会调用

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application

 

posted @ 2014-01-06 22:06  Rynn  阅读(438)  评论(0编辑  收藏  举报