swift编程开发iOS学习笔记iOS学习

iOS - UIAlertController

2016-07-08  本文已影响97人  Mili苗

直入主题,本文将介绍 UIAlertView 和 UIAlertController 。

UIAlertView

代码:

    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Title"
                                                        message:@"message"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"OK", nil];
    [alertview show];

效果图:

注意:苹果官方现在并不提倡在iOS 8中使用UIAlertView,取而代之的是UIAlertController。

UIAlertController

代码:

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title"
                                                                            message:@"message"
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                             
                                                         }];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK"
                                                       style:UIAlertActionStyleDefault
                                                     handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];

效果图:

注意:根据苹果官方制定的《iOS 用户界面指南》,在拥有两个按钮的对话框中,您应当将取消按钮放在左边。

上一篇 下一篇

猜你喜欢

热点阅读