iOS 弹框视图

UIActionSheet UIAlertView UIAlertViewController 的区别

UIActionSheet

  • iOS 8.3 之后过期
  • 模态弹出,显示在试图的底部 类似菜单提供选择
1
2
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"title" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"按钮1", @"按钮2", nil];
[actionSheet showInView:self.view];

UIAlertView

  • iOS 9.0 之后过期
  • 模态弹出,显示在试图的中间位置 类似一个对话框提供选择
1
2
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"考勤方式" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:faceBtn, fingerPrintBtn, nil];
[alertView show];

UIAlertViewController

  • iOS 8.0 起可以使用
  • style : 默认样式, 类似 UIActionSheet 列表形式
  • style 为:UIAlertControllerStyleAlert 对话框形式
  • 设置按钮的样式

如果修改 UIAlertAction 中stye 为:

UIAlertActionStyleCancel
取消按钮会和其他按钮分开

如果修改 UIAlertAction 中stye 为:

UIAlertActionStyleDestructive
取消按钮会以红色警告的方式显示, 但是按钮之间不会分开

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"考勤方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *faceBtn = [UIAlertAction actionWithTitle:@"人脸识别" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }];

UIAlertAction *fingerPrintBtn = [UIAlertAction actionWithTitle:@"指纹认证" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];

UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];

[alert addAction:faceBtn];
[alert addAction:fingerPrintBtn];
[alert addAction:cancelBtn];
[self presentViewController:alert animated:YES completion:nil];

本地缓存

保存图片到沙盒

1
2
3
4
5
6
- (void)saveImage:(UIImage *)image InSanBoxWithIndexPath:(NSInteger)indexPath {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_%ld.png", indexPath]]; // 保存文件的名称
[UIImagePNGRepresentation(image)writeToFile:filePath atomically:YES];
}

获取沙盒中的图片

1
2
3
4
5
6
7
- (UIImage *)fetchImageFromSanBoxWithIndexPath:(NSInteger)indexPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_%ld.png", indexPath]]; // 保存文件的名称
UIImage *img = [UIImage imageWithContentsOfFile:filePath];
return img;
}

删除沙盒中的图片

1
2
3
4
5
6
7
8
9
10
11
12
- (BOOL)deleteImageAtIndexPath:(NSInteger)index;
{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_%ld.png", index]];

if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
return [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}

return NO;
}

修改图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
- (UIImage *)fixOrientation:(UIImage *)aImage {

// No-op if the orientation is already correct
if (aImage.imageOrientation == UIImageOrientationUp)
return aImage;

// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransform transform = CGAffineTransformIdentity;

switch (aImage.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;

case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;

case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
default:
break;
}

switch (aImage.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;

case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
default:
break;
}

// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
CGImageGetBitsPerComponent(aImage.CGImage), 0,
CGImageGetColorSpace(aImage.CGImage),
CGImageGetBitmapInfo(aImage.CGImage));
CGContextConcatCTM(ctx, transform);
switch (aImage.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
break;

default:
CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
break;
}
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}

SQL 使用

常用SQL 语句

创建一张表

create table if not exists t_product(productId integer, productName text, productPrice real);

删除一张表

drop table if exists t_product;

插入一条记录

如果不写某个字段和对应值就会为NUll

insert into t_product(productId, productName, productPrice) values (0001, 'iPhone7', 6100.8);

修改记录

update t_product set productId = 20002 where productName = 'iPhone5s';

删除

delete from t_product where productName = 'iphone5s';

注意:条件语句不能单独存在,只能在修改/删除/查询之后

简单查询

准备数据生成sql语句文件

NSMutableString *mutableString = [NSMutableString string];
int productIdValue = arc4random_uniform(1000);
NSString *productNameValue = [NSString stringWithFormat:@"iPhone_%d",1 + arc4random_uniform(8)];
CGFloat productPriceValue = 3000 + arc4random_uniform(1000);

for (NSInteger i = 0; i < 1000; i++) {
    [mutableString appendFormat:@"insert into t_product(productId, productName, productPrice) values (%d, %@, %f); \r\n", productIdValue, productNameValue, productPriceValue];
}

// 写入文件
[mutableString writeToFile:@"/Users/Apeng/work/Demo/Sqlite/products.sql" atomically:YES encoding:NSUTF8StringEncoding error:NULL];

生成的sql语句文件

SQL 查询语句

select *(表示字段)from t_product(表名)where(条件语句) productId > 100 (字段满足的条件) ;

select * from t_product where productPrice < 5000; 


or: 或

and: 且

分页查询

limit 0,5 ( limit 索引,每页取得数据条数 )

第一页 limit 0,5;

第二页 limit 5,5;

第三页 limit 10,5;

第四页 limit 15,5;

第N页 limit (N - 1)* 5

select * from t_product where productName = 'iPhone_8' and productPrice < 3300 order by productPrice limit 5,5;

排序查询

order by 默认是从小到大,一般是在某一个结果之后

默认升序

// 默认升序
select * from t_product where productName = 'iPhone_8' and productPrice < 3300 order by productPrice;

// 降序
select * from t_product where productName = 'iPhone_8' and productPrice < 3300 order by productPrice desc;

模糊查询

关键字搜索 ,% 是指通配符

SQL 顺序

select * from 表名 条件语句(模糊匹配)排序语句 分页语句

select * from t_product where productName like '%_4' order by productPrice limit 0,5;

主键

数据库的约定俗称,自增长,建议创建表时增加主键,也可以之后再设计表

CREATE TABLE IF NOT EXISTS t_class (id integer PRIMARY KEY, className text, classNO integer); 

外键约束

当两张表有关联时,需要使用外键约束,一张表中的字段名所对应的值只能来自于另一张表中

添加外键约束之后的变化

多表查询

t_department(部门表)和 t_employee(员工表)两张表

需求:在 t_employee 表中查出部门研发部的人员

1 嵌套查询:

先查根据部门名称查出部ID,

然后再用部门ID查出员工表中的员工

以上两步的代码可合并

根据部门名称查出部门ID

SELECT id from t_department WHERE departmentName = '研发部';

根据部门id查出这个部门的人员

SELECT * from t_employee WHERE departmentID = 1;

合并后为

select * from t_employee where departmentID = (SELECT id from t_department WHERE departmentName = '研发部'); 

结果为:

  1. 链接查询

给表起别名,给字段起别名

select employee.*, depart.departmentName deptN from t_department depart, t_employee employee;

/*多表查询之链接查询/

select t_employee.*, t_department.departmentName FROM t_department, t_employee;

消除笛卡尔积

select employee.*, depart.departmentName deptN from t_department depart, t_employee employee where employee.departmentID = depart.id and employee.departmentID = 1;