日日爱影视_日本一区二区三区日本免费_大香焦伊人在钱8_欧美一级夜夜爽 - 日韩三级视频在线观看

當前位置:首頁 ? 做APP ? 正文

ios uialertviewcontroller封裝是什么意思?

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 prangular 打包成apkesentViewController: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:^{

// 處理取消按鈕的操作

}];

“`

在上面的代碼中,我們創建了一個安卓轉iosappCustomAlertViewController的實例,設置了標題、消息、確認按鈕和取消按鈕的標題,然后調用showWithConfirmBlock:cancelBlock:方法顯示該彈窗。在回調塊中,我們可以編寫處理確認和取消按鈕事件的代碼。

總結

通過對UIAlertViewController進行封裝,我們可以方便地復用該控件,并且可以將其與處理事件的代碼分離,使得代碼更加清晰和易于維護。在實際開發中,我們可以根據需要對CustomAlertViewController進行擴展,增加更多的屬性和方法,以滿足不同的需求。

未經允許不得轉載:http://www.glwnet.com/智電網絡 ? ios uialertviewcontroller封裝是什么意思?

相關推薦

推薦欄目