ios uiwindow弹窗_iOS 你需要的弹窗大全

本文介绍了如何在iOS应用中自定义弹窗,从使用系统的AlertViewController到自定义代理和Block回调的方法,详细展示了创建自定义弹窗的步骤,包括设置标题、消息、按钮以及回调处理。
摘要由CSDN通过智能技术生成

在我们的实际开发项目中,弹窗是必不可少的,很多时候我们用的是系统的AlertViewController,但是实际情况中,并不能满足我们的开发需求,这个时候我们需要的就是自定义自己的弹窗效果。接下来我会写一些自己的所封装的弹窗效果。包括代理delegate回调,block 回调,xib新建view来创建我们需要的弹窗效果。

官方思路

1.在我们自己动手之前一定要先看看官方是怎么封装的,这样我们写出来的代码才接近苹果语言,看起来高大上。好的代码一定是见名知意的,别人一看这个方法就知道大概我们通过这个方法可以得到什么样的效果。

// ios8.0 之后

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"message" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"确定");

}];

[alertController addAction:cancelAction];

[alertController addAction:okAction];

[self presentViewController:alertController animated:YES completion:nil];

// ios8.0 之前

UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Tittle" message:@"This is message" delegate:self

cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil];

[alertView show];

因为在代码量风格上,我还是比较喜欢老版本的弹窗,毕竟代码上啊,一句话调用美滋滋。所以接下来我们封装也是模仿官方开始.....

delegate

我们可以看到在苹果官方中,我们需要通过识别用户点击某个按钮来确定需要进一步的操作事件,这个时候是通过代理来实现的。代理的话,我们在熟悉不过了。

首先申明协议

#pragma mark - 协议

@class HLAlertView;

@protocol HLAlertViewDelegate

- (void)alertViewDidClickButtonWithIndex:(NSInteger)index;

@end

在viewController中遵循代理,设置代理 , 实现方法即可<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值