iOS UIAlertController学习笔记

在最近学习的iOS开发中用到了一些警告提示框,看到的很多教程里用的还是UIAlertview,但是iOS9.0开始,xcode里已经明确提示了不推荐使用,而是用UIAlertController来代替。
记录一下我用到的几种情况:

1.普通的警告提示框,显示在屏幕正中心,有一个标题,一段内容,以及一个确定按钮和一个取消按钮:

NSString *title = @"这是标题";  
NSString *message = @"这是提示内容";  
NSString *cancelButtonTitle = NSLocalizedString(@"取消", nil);  
NSString *otherButtonTitle = NSLocalizedString(@"确定", nil);  
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title  
                                                                         message:message  
                                                                  preferredStyle:UIAlertControllerStyleAlert];  
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle  
                                                       style:UIAlertActionStyleCancel  
                                                     handler:^(UIAlertAction *action) {}];  
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle  
                                                      style:UIAlertActionStyleDefault  
                                                    handler:^(UIAlertAction *action) {  
                                                        [self startAgain];  
                                                    }];  
[alertController addAction:cancelAction];  
[alertController addAction:otherAction];  
[self presentViewController:alertController animated:YES completion:nil]; 

当然也可以根据实际情况去掉一个按钮。

2.从下方向上显示的,多用于选择相机还是相册这类情况:

UIAlertController *_alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[_alertController addAction:[UIAlertAction actionWithTitle:@"拍摄" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    [self takePhoto];
}]];
[_alertController addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    [self choosePhotoFromLibrary];
}]];
[_alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    _alertController = nil;
}]];
[self presentViewController:_alertController animated:YES completion:nil];

暂时我就用到了这两种情况,当然还有带一个输入框的,以及多个输入框的,带有警示按钮的等等,这些都还没用到,等用到了再记录下来。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,252评论 4 61
  • iOS 8的新特性之一就是让接口更有适应性、更灵活,因此许多视图控制器的实现方式发生了巨大的变化。全新的UIPre...
    乌拉拉zzZ阅读 945评论 0 2
  • 今天听了好几遍成甲讲的《微习惯》这本书,发现微习惯跟自控力关系匪浅,立即跟大家分享之。 这本书的作者斯蒂芬盖斯不是...
    肖爷_族长阅读 582评论 6 11
  • 2017.06.12 天气阴晴不定 最近很焦躁,心里总是隐隐藏着一座活火山,一直叫嚣着要爆发。手上的佛珠捏...
    violefay阅读 521评论 0 1
  • 乌金: 你好。一直以来,我觉得说话是要表达心声的,尤其是在自己的地盘。微信朋友圈算是自个儿的地盘吧,在自己的地盘聊...
    彼得猫乌金阅读 316评论 2 48