封装 iOS UIAlertViewController:简化Alert操作,提高用户体验
本文将介绍如何封装 iOS UIAlertViewController,简化Alert操作并提高用户体验。通过自定义AlertViewController的外观和功能,用户可以更加轻松地使用Alert功能。通过本文的示例代码,您将学习怎样达成自定义AlertView的样式和行为,以满足您的应用程序需求。除此之外,您还将了解如何将封装后的AlertViewController集成到您的应用程序中,以提高整体用户体验。
UIAlertViewController是iOS开发中常用的一个弹窗控件,可以用来显示提示信息、警告信息、确认信息等等。在实际开发过程中,我们可能需要多次使用UIAlertViewController,为了方便代码复用,我们可将UIAlertViewController进行封装,以便在需要使用时直接调用。
UIAlertViewController的封装原理主如果通过创建一个继承自UIAlertViewController的类,并在该类中编写需要的方法和属性。在需要使用UIAlertViewController时,我们只需要创建该类的实例,然后调用其中的方法即可。
下面,我们来详细介绍如何进行UIAlertViewController的封装。
1. 创建一个继承自UIAlertViewController的类
首先,我们需要创建一个继承自UIAlertViewController的类,命名为CustomAlertViewController。在该类中,我们可定义需要的属性和方法。
```
@interface CustomAlertViewController : UIAlertController
@property (nonatomic, copy) void(^confirmBlock)(void);
@property (nonatomic, copy) void(^cancelBlock)(void);
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message confirmTitle:(NSString *)confirmTitle cancelTitle:(NSString *)cancelTitle;
@end
```
在上面的代码中,我们定义了三个属性和一个初始化方法。其中,confirmBlock和cancelBlock是两个回调块,用来处理用户点击确认和取消按钮的操作。initWithTitle: message: confirmTitle: cancelTitle:方法用来初始化UIAlertViewController的标题、消息、确认按钮和取消按钮的标题。
2. 实现初始化方法
在CustomAlertViewController.m文件中,我们需如果要实现initWithTitle: message: confirmTitle: cancelTitle:方法。在该方法中,我们可通过调用父类的initWithTitle: message: preferredStyle:方法来初始化UIAlertViewController,并设置确认和取消按钮的事件。
```
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message confirmTitle:(NSString *)confirmTitle cancelTitle:(NSString *)cancelTitle {
self = [super initWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
if (self) {
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:confirmTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * ,Nonnull action) {
if (self.confirmBlock) {
self.confirmBlock();
}
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * ,Nonnull action) {
if (self.cancelBlock) {
self.cancelBlock();
}
}];
[self addAction:confirmAction];
[self addAction:cancelAction];
}
return self;
}
```
在上面的代码中,我们创建了两个UIAlertAction对象,分别用来表示确认和取消按钮,并设置它们的事件。然后,我们调用了addAction:方法,将两个UIAlertAction对象添加到UIAlertViewController中。
3. 实现回调块
在CustomAlertViewController类中,我们定义了两个回调块confirmBlock和cancelBlock,用来处理用户点击确认和取消按钮的操作。在实际使用中,我们需要对这两个回调块进行实现。
```
- (void)showWithConfirmBlock:(void (^)(void))confirmBlock cancelBlock:(void (^)(void))cancelBlock {
self.confirmBlock = confirmBlock;
self.cancelBlock = cancelBlock;
UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootVC presentViewController:self animated:YES completion:nil];
}
```
在上面的代码中,我们定义了一个名为showWithConfirmBlock:cancelBlock:的方法,该方法用来显示UIAlertViewController并设置确认和取消按钮的事件。在该方法中,我们将confirmBlock和cancelBlock赋值给CustomAlertViewController类中定义的两个回调块,并获取当前应用程序的根视图控制器,然后调用presentViewController:animated:completion:方法显示UIAlertViewController。
4. 使用自定义弹窗
在实际使用CustomAlertViewController时,我们只需要创建该类的实例,并调用showWithConfirmBlock:cancelBlock:方法即可。
```
CustomAlertViewController *alertVC = [[CustomAlertViewController alloc] initWithTitle:@提示 message:@确定要退出登入吗? confirmTitle:@确定 cancelTitle:@取消];
[alertVC showWithConfirmBlock:^{
// 处理确认按钮的操作
} cancelBlock:^{
// 处理取消按钮的操作
}];
```
在上面的代码中,我们创建了一个CustomAlertViewController的实例,设置了标题、消息、确认按钮和取消按钮的标题,然后调用showWithConfirmBlock:cancelBlock:方法显示该弹窗。在回调块中,我们可编写处理确认和取消按钮事件的代码。
总结
按照对UIAlertViewController进行封装,我们可方便地复用该控件,并且可以把它与处理事件的代码分离,使得代码更加清晰和易于维护。在实际开发中,我们可根据需要对CustomAlertViewController进行扩展,增加更多的属性和方法,从而满足不同的需求。