自定义UIAlertView

本文介绍如何通过重写 dismissWithClickedButtonIndex 方法来自定义 UIAlertView 的行为,使其即使在按钮被点击后也不会消失,适用于需要强制更新等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

IOS8.0之前使用UIAlertView作为系统默认的弹出提示框,IOS8.0后使用UIAlertController替代。相比较UIAlertController功能确实强大,多很多,包括集成了UIActionSheet的功能。但是还是比较喜欢使用UIAlertView,因为使用简单,轻量级,不用使用pushViewController来显示。

UIAlertView默认情况下,点击上面的按钮后,UIAlertView会消失;在某些特殊情况比如,强制更新,需要即便是点击的按钮,UIAlertView也不要消失。我们可以重写UIAlertView的dismissWithClickedButtonIndex方法实现该功能:

#import <UIKit/UIKit.h>

@interface CustomAlertView : UIAlertView

@property(nonatomic, assign) BOOL isClickDismiss;

@end


#import "CustomAlertView.h"

@implementation CustomAlertView

-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated{
    if(!_isClickDismiss){
        return;
    }
    [super dismissWithClickedButtonIndex:buttonIndex animated:animated];
}

@end
可以直接根据传递的属性isClickDismiss控制点击按钮后是否消失功能


UIAlertController

UIAlertController

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值