iOS - UIAlertController

作者: Mili苗 | 来源:发表于2016-07-08 13:49 被阅读97次

    直入主题,本文将介绍 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 用户界面指南》,在拥有两个按钮的对话框中,您应当将取消按钮放在左边。

    相关文章

      网友评论

        本文标题:iOS - UIAlertController

        本文链接:https://www.haomeiwen.com/subject/frfrjttx.html