UIAlerView全面解析

一、UIAlerView重复弹

在开发中,经常会遇到连续的弹出提示框的情况,如果不加以处理,可能会出现重复弹出的问题。

解决方法:


// 声明全局变量
@property (nonatomic, assign) BOOL isShowingAlertView;

// 在需要弹出的地方调用该方法
-(void)showAlertView {
    if (self.isShowingAlertView) {
        return;
    }
    self.isShowingAlertView = YES;
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alertView show];
}

// 在结束代理方法里,把isShowingAlertView设置为NO
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    self.isShowingAlertView = NO;
}

二、UIAlerViewController

从iOS8开始,UIAlertView已经被苹果推出的UIAlerViewController所替代。

三、UIAlerView主动弹出

在某些情况下,可能需要程序主动弹出UIAlerView,比如用户输入的信息不合法,需要给予提示。

解决方法:


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

四、UIAlerView字体颜色

UIAlerView的字体颜色默认是黑色的,如果需要改变字体颜色,可以通过设置富文本来实现。

解决方法:


UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
NSMutableAttributedString *alertString = [[NSMutableAttributedString alloc]initWithString:@"Message"];
[alertString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 7)]; // 从第0个字符开始,长度为7个字符
[alertView setValue:alertString forKey:@"attributedMessage"];
[alertView show];

五、UIAlerViewController自定义

UIAlerViewController提供了自定义的接口,可以根据自己的需要来进行布局和样式的调整。

解决方法:


UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
UIView *subView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 100)];
[subView setBackgroundColor:[UIColor yellowColor]];
[alertController.view addSubview:subView];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];

六、UIAlerViewController苹果

苹果在UIAlerViewController中提供了一些默认的样式,包括UIAlertControllerStyleAlert和UIAlertControllerStyleActionSheet。

解决方法:


UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];

七、UIAlerViewController竖排了

有些情况下,UIAlerViewController中的按钮可能会变成竖排的,这是由于按钮过多而导致的,可以通过设置preferredAction属性来指定主按钮,避免出现竖排情况。

解决方法:


UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDestructive handler:nil];
// 设置preferredAction属性
[alertController addAction:cancelAction];
[alertController addAction:destructiveAction];
[alertController addAction:okAction];
alertController.preferredAction = okAction;
[self presentViewController:alertController animated:YES completion:nil];

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/238238.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝的头像小蓝
上一篇 2024-12-12 12:10
下一篇 2024-12-12 12:10

相关推荐

  • Python应用程序的全面指南

    Python是一种功能强大而简单易学的编程语言,适用于多种应用场景。本篇文章将从多个方面介绍Python如何应用于开发应用程序。 一、Web应用程序 目前,基于Python的Web…

    编程 2025-04-29
  • Python zscore函数全面解析

    本文将介绍什么是zscore函数,它在数据分析中的作用以及如何使用Python实现zscore函数,为读者提供全面的指导。 一、zscore函数的概念 zscore函数是一种用于标…

    编程 2025-04-29
  • 全面解读数据属性r/w

    数据属性r/w是指数据属性的可读/可写性,它在程序设计中扮演着非常重要的角色。下面我们从多个方面对数据属性r/w进行详细的阐述。 一、r/w的概念 数据属性r/w即指数据属性的可读…

    编程 2025-04-29
  • Python计算机程序代码全面介绍

    本文将从多个方面对Python计算机程序代码进行详细介绍,包括基础语法、数据类型、控制语句、函数、模块及面向对象编程等。 一、基础语法 Python是一种解释型、面向对象、动态数据…

    编程 2025-04-29
  • Matlab二值图像全面解析

    本文将全面介绍Matlab二值图像的相关知识,包括二值图像的基本原理、如何对二值图像进行处理、如何从二值图像中提取信息等等。通过本文的学习,你将能够掌握Matlab二值图像的基本操…

    编程 2025-04-28
  • 疯狂Python讲义的全面掌握与实践

    本文将从多个方面对疯狂Python讲义进行详细的阐述,帮助读者全面了解Python编程,掌握疯狂Python讲义的实现方法。 一、Python基础语法 Python基础语法是学习P…

    编程 2025-04-28
  • 全面解析Python中的Variable

    Variable是Python中常见的一个概念,是我们在编程中经常用到的一个变量类型。Python是一门强类型语言,即每个变量都有一个对应的类型,不能无限制地进行类型间转换。在本篇…

    编程 2025-04-28
  • Zookeeper ACL 用户 anyone 全面解析

    本文将从以下几个方面对Zookeeper ACL中的用户anyone进行全面的解析,并为读者提供相关的示例代码。 一、anyone 的作用是什么? 在Zookeeper中,anyo…

    编程 2025-04-28
  • Python合集符号全面解析

    Python是一门非常流行的编程语言,在其语法中有一些特殊的符号被称作合集符号,这些符号在Python中起到非常重要的作用。本文将从多个方面对Python合集符号进行详细阐述,帮助…

    编程 2025-04-28
  • Switchlight的全面解析

    Switchlight是一个高效的轻量级Web框架,为开发者提供了简单易用的API和丰富的工具,可以快速构建Web应用程序。在本文中,我们将从多个方面阐述Switchlight的特…

    编程 2025-04-28

发表回复

登录后才能评论