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
* NSComparisonResult有3种取值:
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
iOS基础 - 完善键盘处理的更多相关文章
- iOS基础问答面试
<简书社区 — Timhbw>iOS基础问答面试题连载(一)-附答案:http://www.jianshu.com/p/1ebf7333808d <简书社区 — Timhbw> ...
- Xamarin的不归路-ios模拟器没有键盘
ios模拟器没有键盘解决方案: 勾选上就有了. 2016年9月1日
- IOS基础学习-2: UIButton
IOS基础学习-2: UIButton UIButton是一个标准的UIControl控件,UIKit提供了一组控件:UISwitch开关.UIButton按钮.UISegmentedContro ...
- iOS 基础日记-修饰符
今晚随便温习了一下iOS 基础关于修饰符这块的东西,下面简单的来描述一下,其中有的也是在网络学习到的: strong与weak是由ARC新引入的对象变量属性 ARC的解释:ARC引入了新的对象的生命周 ...
- [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)
A.概述 在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能 1.按钮点击后,显示为“已下载”,并且不 ...
- [置顶] IOS 基础入门教程
IOS 基础入门教程 教程列表: IOS 简介 IOS环境搭建 Objective C 基础知识 创建第一款iPhone应用程序 IOS操作(action)和输出口(Outlet) iOS - 委托( ...
- ios如何判断键盘是否已经显示
ios如何判断键盘是否已经显示 在群里看到有人问:ios如何判断键盘已经显示在界面上. 其实这个解决很简单: 写一个单例来管理键盘的状态. 这个单例在初始化方法init种监听2个事件,分别是 UI ...
- iOS 基础:Frames、Bounds 和 CGGeometry
https://segmentfault.com/a/1190000004695617 原文:<iOS Fundamentals: Frames, Bounds, and CGGeometry& ...
- IOS中input键盘事件支持的解决方法
欢迎大家去我的网站详细查看http://genghongshuo.com.cn/ IOS中input键盘事件keyup.keydown.等支持不是很好, 用input监听键盘keyup事件,在安卓手机 ...
随机推荐
- Redis集群环境安装指南
环境 RHLinux-6.4-64-EN, 红帽6.4 64位,英文正式公布版. Redis3.0.0 redis2.x版本号还不支持集群,3.0版本号将会支持,如今3.0版本号还在开发中,如今是be ...
- Matlab曲面拟合和插值
插值和拟合都是数据优化的一种方法,当实验数据不够多时常常须要用到这样的方法来绘图. 在matlab中都有特定的函数来完毕这些功能. 这两种方法的确别在于: 当測量值是准确的,没有误差时,一般用插值: ...
- Codeforces548D:Mike and Feet(单调栈)
Mike is the president of country What-The-Fatherland. There are n bears living in this country besid ...
- TableLayout中stretchColumns、shrinkColumns的使用方法
android:stretchColumns="1" android:shrinkColumns="1"这两个属性是TableLayout所特有的,也是这两个属 ...
- 经典Loading 动漫赏析
0. 传统的菊花Loading动画 假设作为程序猿的你还在用菊花作为Loading动画,是时候换个口味了.来看下5种不错的Loading动画. 几种颜色组成的动画,给用户一个美感. 1.android ...
- s2sh三大框架整合过程(仅供参考)
三大框架顾名思义就是非常有名的Struts2 ,Hibernate,Spring, 框架整合的方法很多,现在我写一个非常简单的整合过程,相信大家一看就会! 这里使用的struts-2.2.1.1.hi ...
- Java Persistence with MyBatis 3(中国版) 第五章 与Spring集成
MyBatis-Spring它是MyBatis子模块框.它用来提供流行的依赖注入框架Spring无缝集成. Spring框架是一个基于依赖注入(Dependency Injection)和面向切面编程 ...
- 转:ShellExecute函数与ShellExecuteEx函数
ShellExecute函数 ShellExecute函数原型及參数含义例如以下: function ShellExecute(hWnd: HWND; Operation, FileName, Par ...
- 快速构建Windows 8风格应用19-基础控件II
原文:快速构建Windows 8风格应用19-基础控件II 本篇博文接着上篇博文<快速构建Windows 8风格应用18-基础控件I>介绍开发Windows 8风格应用中常用控件. Sli ...
- Lyx输入中文与代码高亮
如果您看了我的这个随笔:<OpenSUSE 13.2安装Texlive2014+Texmaker+Lyx> (一)LyX中文 打开Lyx直接新建开始使用,那么输入的中文会是编译失败的,疑? ...