您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 兩大提示控制項:UIAlertView 與 UIActionSheet

編輯:iOS 開發綜合

兩者的差異

UIAlertView 會在螢幕中央彈出一個方形的提示框,可以單純用來顯示訊息,也能提供多個選項讓使用者挑選。

UIActionSheet 則是從螢幕底部滑出,功能與 UIAlertView 相似,但除了位置不同之外,外觀樣式也有明顯差異,比較適合呈現「動作選單」。

要接收兩者的互動結果,必須設定代理,並讓代理類別採用對應的協定(UIAlertViewDelegate、UIActionSheetDelegate),然後實作以下兩個方法之一:

// 按鈕被點擊時呼叫,回傳後視圖會自動關閉
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;

讓代理類別採用協定

@interface ViewController : UIViewController <UIAlertViewDelegate, UIActionSheetDelegate>

主要程式碼

- (IBAction)onTextFieldEnd:(id)sender {
    [_textTime resignFirstResponder];

#if 0
    // 中央彈出的提示框
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TextContend"
                                                   message:_textTime.text
                                                  delegate:self
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles:@"Good", @"Thanks", nil];
    [alert show];
    [alert release];
#else
    // 底部彈出的動作表單
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"ActionSheet"
                                                      delegate:self
                                             cancelButtonTitle:@"cancelButtonTitle"
                                        destructiveButtonTitle:@"destructiveButtonTitle"
                                             otherButtonTitles:@"OtherButton1", @"OtherButton2", nil];
    [sheet showInView:self.view];
    [sheet release];
#endif
}

// UIAlertView 的按鈕回呼
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *strInfo = [[NSString alloc] initWithFormat:@"你按下了【%@】按鈕,第 %d 個按鈕",
                         [alertView buttonTitleAtIndex:buttonIndex], buttonIndex];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"按鈕回呼代理方法"
                                                   message:strInfo
                                                  delegate:nil
                                         cancelButtonTitle:@"確定"
                                         otherButtonTitles:nil];
    [alert show];
    [alert release];
    [strInfo release];
}

// UIActionSheet 的按鈕回呼
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *strInfo = [[NSString alloc] initWithFormat:@"你按下了【%@】按鈕,第 %d 個按鈕",
                         [actionSheet buttonTitleAtIndex:buttonIndex], buttonIndex];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"按鈕回呼代理方法"
                                                   message:strInfo
                                                  delegate:nil
                                         cancelButtonTitle:@"確定"
                                         otherButtonTitles:nil];
    [alert show];
    [alert release];
    [strInfo release];
}

兩個回呼的寫法幾乎一致,差別只在於用哪個物件去問 buttonTitleAtIndex:。實務上若要區分是哪一個來源觸發,可以在建立時把 delegate 拆成不同物件,或改用 tag 標記。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved