will在动作发生前,委托可以对动作做出响应,但不带有返回值。
did在动作发生后做出的响应。
instancetype和id的异同
1、相同点
都可以作为方法的返回类型
2、不同点
①instancetype可以返回和方法所在类相同类型的对象,id只能返回未知类型的对象;
②instancetype只能作为返回值,不能像id那样作为参数
1、instancetype只能用于方法的返回类型,而id用处和NSObject *类似。
2、instancetype会告诉编译器当前的类型,这点和NSObject *类似,但id对于编译器却是无类型的,调用任何方法不会给出错误提示。
3、对于init方法,id和instancetype是没有区别的。因为编译器会把id优化成instancetype。当明确返回的类型就是当前Class时,使用instancetype能避免id带来的编译不出的错误情况。
4、NSObject Class和id都是仅包含了一个Class isa。但NSObject 包含了更多方法的定义。
5、id和instancetype都能省去具体类型,提高代码的通用性。而这是NSObject *不及的。
IBOutLet 与IBAction
在图形界面编程时,解决的第一问题就是如何将静态界面与代码关联起来,或者说是代码如何与界面上的对象
通信, 代码如何操作界面上的对象。在iPhone平台上,引入了IBOutlet与IBAction。通过在变量前增加IBOutlet
来说明该变量将与界面上的某个UI对象对应,在方法前增加IBAction来说明该方法将与界面上的事件对应.
CGFloat: 浮点值的基本类型
CGPoint: 表示一个二维坐标系中的点
CGSize: 表示一个矩形的宽度和高度
CGRect: 表示一个矩形的位置和大小
typedef float CGFloat;// 32-bit typedef double CGFloat;// 64-bit struct CGPoint { CGFloat x; CGFloat y; }; typedef struct CGPoint CGPoint; struct CGSize { CGFloat width; CGFloat height; }; typedef struct CGSize CGSize; struct CGRect { CGPoint origin; CGSize size; }; typedef struct CGRect CGRect;
注意:CGRect数据结构的高度和宽度可以是负数。例如,一个矩形的原点是[0.0,0.0]和大小是[10.0,10.0]。这个矩形完全等同原点是[10.0,10.0]和大小是[-10.0,-10.0]的矩形。
2、使用值来创建几何元素的方法
CGPointMake
CGRectMake
CGSizeMake
CGPoint CGPointMake ( CGFloat x, CGFloat y ); CGSize CGSizeMake ( CGFloat width, CGFloat height ); CGRect CGRectMake ( CGFloat x, CGFloat y, CGFloat width, CGFloat height );
CGFloat ten=10.0f; CGPoint point = CGPointMake(0.0f, 0.0f); CGSize size = CGSizeMake(10.0f, 10.0f); CGRect rect = CGRectMake(point.x, point.y, size.width, size.height); NSLog(@"ten: %f", ten); NSLog(@"point: %@", NSStringFromCGPoint(point)); NSLog(@"size: %@", NSStringFromCGSize(size)); NSLog(@"rect: %@", NSStringFromCGRect(rect));
</pre><pre name="code" class=" c" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; box-sizing: border-box; color: rgb(75, 75, 75); font-size: 13px; line-height: 20.7999992370605px; background-color: rgb(255, 255, 255);">常用方法:
[sender resignFirstResponder] :放弃作为第一响应对象, 文本框的 Did End On Exit 事件关联此代码后可以关闭键盘。将视图容器的父类由 UIView 改为 UIControl 然后 实现 Touch Down 事件对所有文本框调用 此方法 可关闭键盘。
控件状态:
每个IOS控件都有如下4种状态,任何时候都处于并只能处于其中的一种状态。
Normal 普通
Highlighted 突出显示
Disabled 禁用
Selected 选中
UISegmentedControl.selectedSegmentIndex 表示选中的分段 left ==0 right ==1
</pre><pre name="code" class=" c" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; box-sizing: border-box; color: rgb(75, 75, 75); font-size: 13px; line-height: 20.7999992370605px; background-color: rgb(255, 255, 255);">
- (NSUInteger)supportedInterfaceOrientations; 返回单个控制器支持的自动旋转方向, 全局的支持旋转方向的配置在General中。
</pre><pre name="code" class=" c" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; box-sizing: border-box; color: rgb(75, 75, 75); font-size: 13px; line-height: 20.7999992370605px; background-color: rgb(255, 255, 255);">动画
[UIView beginAnimations: ];
[UIView setAnimationDuration ];
[UIView setAnimationCurve ];
[UIView setAnimationTransition ];
[UIView commitAnimations];
</pre><pre name="code" class=" c" style="margin-top: 0px; margin-bottom: 0px; word-wrap: break-word; box-sizing: border-box;">获取 Document目录路径:
NSArray *paths = NSSearchPathForDirectoritesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentDirectory = path[0];
该目录下的文件路径:
NSString *fileName = [documentDirectory stringByAppendingPathComponent:@"theFile.txt"];
</pre><pre name="code" class=" c" style="margin-top: 0px; margin-bottom: 0px; word-wrap: break-word; box-sizing: border-box;">获取tmp目录:
NSString *temPath = NSTemporaryDirectory();
</pre><pre name="code" class=" c" style="margin-top: 0px; margin-bottom: 0px; word-wrap: break-word; box-sizing: border-box;">
</pre><pre name="code" class=" c" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; box-sizing: border-box; color: rgb(75, 75, 75); font-size: 13px; line-height: 20.7999992370605px; background-color: rgb(255, 255, 255);">
</pre><pre name="code" class=" c" style="margin-top:0px; margin-bottom:0px; white-space:pre-wrap; word-wrap:break-word; color:rgb(75,75,75); font-size:13px; line-height:20.7999992370605px; background-color:rgb(255,255,255)">
NSCoding协议声明了两个方法:
-(void)encodeWithCoder:(NSCoder *)aCoder,是将对象写入到文件中。
-(id)initWithCoder:(NSCoder *)aDecoder,是将文件中数据读入到对象中。
bringSubviewToFront()方法可以将指定的视图推送到前面,而 sendSubviewToBack()则可以将指定的视图推送到背面
明确数值的常量用 const 定义(如 NSString const name = @"name";) 需要获取的常量用 define定义 (如 bounds.size.width)