Objective-C利用AFN上传头像、UIAlertController的使用
2016-04-11 12:53
537 查看
iOS8推出了一个新的“controller”,主要是把类似之前的UIAlertView变成了UIAlertController,把UIAlertView和UIActionSheet二合一。
按钮响应方法弃用了代理而绑定使用了block方式,使可读性更强,不必在两个地方写代码了,但也需要注意循环引用的问题:
AlertView:
Actionsheet:
此外上传头像访问相册和拍照的方法也很简单,UIImagePickerController很容易实现,但需要注意权限的问题。上传到服务器则直接调用AFN的post请求即可,具体代码可参考demo。
demo效果:
GitHub:https://github.com/FEverStar/UploadHeadImage
按钮响应方法弃用了代理而绑定使用了block方式,使可读性更强,不必在两个地方写代码了,但也需要注意循环引用的问题:
__block typeof (self) weak_self = self;向UIAlertController里面添加UIAlertAction的方式实现之前的UIActionSheet控件的功能,把[alert show]的方法改成了controller的presentViewController形式。这里借着上传头像的demo介绍一下这个新的提示框的用法。
AlertView:
NSString *tips = @"相机不可用"; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"无法使用相机" message:tips preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"取消"); }]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSLog(@"确定"); }]; [alertController addAction:cancelAction]; [alertController addAction:otherAction]; [self presentViewController:alertController animated:YES completion:nil];
Actionsheet:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请选择图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {}]; UIAlertAction* fromPhotoAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { NSLog(@"从相册选择"); }]; UIAlertAction* fromCameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { NSLog(@"相机"); }]; [alertController addAction:cancelAction]; [alertController addAction:fromCameraAction]; [alertController addAction:fromPhotoAction]; [self presentViewController:alertController animated:YES completion:nil];
此外上传头像访问相册和拍照的方法也很简单,UIImagePickerController很容易实现,但需要注意权限的问题。上传到服务器则直接调用AFN的post请求即可,具体代码可参考demo。
demo效果:
GitHub:https://github.com/FEverStar/UploadHeadImage
相关文章推荐
- Objective-C的对象模型和runtime机制
- Objective-C语法之KVO使用
- "A MultiPath Network for Object Detectio" 阅读笔记
- ArrayList的contains方法与Object的equals方法的关系
- The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
- 'ModelOptions' object has no attribute 'get_field_names
- Object -c 单例模式
- iOS Objective-C基本核心动画,偏移,旋转,缩放,路径,抖动,组动画
- Swift项目中调用Objecttive-C
- 什么是writeObject 和readObject?可定制的序列化过程(转)
- iOS开发67- valueForKey:和objectForKey:的区别
- libcudnn.so.6.5:cannot open sharedobject file: No such file or directory
- Objective-C中nil与release的区别与用法
- objective-c中自己创建的对象为什么不能调用release
- poj 3241 Object Clustering 曼哈顿最小生成树
- Swift项目里使用Objective-C第三方库的总结
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- Object类到底实现了哪些方法
- 重构第23天 引用参数对象(Introduce Parameter Object)
- Object-C 介绍