全部產品
Search
文件中心

Mobile Platform as a Service:提示彈窗

更新時間:Jul 13, 2024

AUNoticeDialog 為普通對話方塊樣式,參考自系統 AlertView 但是不帶 blur 背景。對話方塊的 Window 層級邏輯為 self.windowLevel = UIWindowLevelAlert - 1

效果圖

介面說明

 /**
 普通 Dialog ,同系統樣式不帶 blur 背景
 */
@interface AUNoticeDialog : AUDialogBaseView

/**
 不帶按鈕標題的初始化方法。

 @param title 標題
 @param message 訊息內容
 @return AUNoticeDialog 執行個體
 */
- (instancetype)initWithTitle:(NSString *)title
                                            message:(NSString *)message;

/**
 帶按鈕標題的初始化方法。

 @param title 標題
 @param message 訊息內容
 @param delegate 協議對象(遵循 AUDialogDelegate)
 @param buttonTitle 按鈕標題列表
 @return AUNoticeDialog 執行個體
 */
- (instancetype)initWithTitle:(NSString *)title
                                            message:(NSString *)message
                                        delegate:(id<AUDialogDelegate>)delegate
                                buttonTitles:(NSString *)buttonTitle, ... NS_REQUIRES_NIL_TERMINATION;

- (instancetype)initWithCustomView:(UIView *)customView; // 自訂內容地區

- (instancetype)init NS_UNAVAILABLE;

/**
 Dialog 展示方法。
 */
- (void)show;

/**
 添加按鈕以及其回調方法。

 @param buttonTitle 按鈕標題
 @param actionBlock 按鈕點擊回調
 */
- (void)addButton:(NSString *)buttonTitle actionBlock:(AUDialogActionBlock)actionBlock;

/**
  Dialog 消失方法,類似 APAlertView 的 dismissWithClickedButtonIndex 方法
  */
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

/**
  設定文本對齊
  @param alignment 對齊參數
  */
- (void)setMessageAlignment:(NSTextAlignment)alignment;

全新接入

  • 使用 block 添加 button 點擊回調:

      AUNoticeDialog *dialog = [[AUNoticeDialog alloc] initWithTitle:@"標題" message:@"內容"];
      [dialog addButton:@"知道了" actionBlock:^{
          NSLog(@"print pressed");
      }];
      [dialog addButton:@"好的" actionBlock:nil];
      [dialog show];
  • 使用 delegate 添加 button 點擊回調:

      AUNoticeDialog *dialog = [[AUNoticeDialog alloc] initWithTitle:@"標題" message:@"內容" delegate:delegate buttonTitles:@"確定", nil];
      [dialog show];
    
      delegate 協議為 AUDialogDelegate(類似 UIAlertViewDelegate)
  • 簡便方法接入:

      NS_INLINE AUNoticeDialog *AUNoticeDialogWithTitle(NSString *title)
      NS_INLINE AUNoticeDialog *AUNoticeDialogWithTitleAndMessage(NSString *title, NSString *message)

APAlertView 與 UIAlertView 接入

以前主要為 APAlertView 和 UIAlertView,本節介紹如何更改為 AUNoticeDialog。

為了更簡單的從 APAlertView 和 UIAlertView 接入 AUNoticeDialog,大部分介面均做了支援,因此大多數情況下只需要更改類名即可,具體如下:

  • AUNoticeDialog 支援 APAlertView 的建立介面。

- (instancetype)initWithTitle:(NSString *)title
                      message:(NSString *)message
                     delegate:(id<AUDialogDelegate>)delegate
            cancelButtonTitle:(NSString *)cancelButtonTitle
            otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;

建立時,更改類名即可,只需將 [[APAlertView alloc] initWithxxxxxx] 改為 [[AUNoticeDialog alloc] initWithxxxxxx]

  • 使用如下方法建立 UIAlertView,無需更改,因為介面中已做了更改。

NS_INLINE UIAlertView *UIAlertViewWithTitleAndMessage(NSString *title, NSString *message)
//
NS_INLINE UIAlertView *UIAlertViewWithTitle(NSString *title)
NS_INLINE UIAlertView *UIAlertViewWithMessage(NSString *message)
  • 支援 APAlertView 的 addButtonWithTitle 介面,接入時 無需更改

- (NSInteger)addButtonWithTitle:(NSString *)title callback:(void (^)(int index, NSString *title))callback;

/**
 @brief 添加取消 Button 和回調
 @param title 按鈕 title
 @param callback 回調的 callback
 */
- (NSInteger)addCancelButtonWithTitle:(NSString *)title callback:(void (^)(int index, NSString *title))callback;

/**
 @brief 添加 Button
 @param title 按鈕 title
 */
- (NSInteger)addButtonWithTitle:(NSString *)title;

/**
 @brief 添加取消 button
 @param title 按鈕 title
 */
- (NSInteger)addCancelButtonWithTitle:(NSString *)title;

+(void)setBackgroundMode:(BOOL)isBackMode;
  • 使用如下 UIAlertView 方法也 無需更改,AUNoticeDialog 有同名方法支援。

/**
Dialog 消失方法,類似 APAlertView 的 dismissWithClickedButtonIndex 方法
*/
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
- (nullable NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
/**
 有多少個按鈕(類似 APAlertView 的 numberOfButtons)
 */
@property(nonatomic,readonly) NSInteger numberOfButtons;

/**
 取消按鈕的 index(類似 APAlertView 的 cancelButtonIndex)
 */
@property(nonatomic) NSInteger cancelButtonIndex;
  • 調用 APAlertView 的如下介面需要變更為其他方法,只需更改方法名

    • showAlert 方法改為 show 方法。

      例如:將 [alertView showAlert] 方法改為 [alertView show] 方法。

    • removeAllAlerviews 方法改為 dismissAll 方法。

      例如:[APAlertView removeAllAlerviews] 方法改為 [AUNoticeDialog dismissAll] 方法。

  • 如果使用了 APAlertView 或者 UIAlertView 的輸入框功能,請使用 AUInputDialog 替換,使用方法與 AUNoticeDialog 基本相同。

    說明

    類檔案為 AUInputDialog.h。

UIAlertController 接入

  • 建立方法修改,例如:

      [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]
      修改為
      [[AUNoticeDialog alloc] initWithTitle:@"標題" message:@"內容"]
  • 添加 button 和事件修改:

      [UIAlertAction actionWithTitle:title style:(UIAlertActionStyle)style handler:handler] 
      修改為 
      [dialog addButton:@"知道了" actionBlock:^{
          NSLog(@"xxxx");
      }]

程式碼範例

  • 標準樣式:

      AUNoticeDialog *dialog = [[AUNoticeDialog alloc] initWithTitle:@"標準控制項" message:@"兩個平台的同類控制項命名需完全一樣,控制項命名以\"AU\"為首碼,控制項自訂屬性全部採用駝峰命名。注意:某些控制項可能存在平台差別,一個平台需要實現另外一個平台不需要實現。"];
      [dialog addButton:@"知道了" actionBlock:nil];
      [dialog addButton:@"好的" actionBlock:nil];
      [dialog show];
  • 自訂樣式:

      UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240, 60)];
      customView.backgroundColor = [UIColor greenColor];
    
      AUNoticeDialog *dialog = [[AUNoticeDialog alloc] initWithCustomView:customView];
      [dialog addButton:@"取消" actionBlock:nil];
      [dialog addButton:@"確定" actionBlock:nil];
      [dialog show];