UIActionSheet的使用

IActionSheet是在IOS彈出的選擇按鈕項,能夠添加多項,併爲每項添加點擊事件。 java

爲了快速完成這例子,咱們打開Xcode 4.3.2, 先創建一個single view application。而後再xib文件添加一個button,用來彈出sheet view。 app

一、首先在.h文件中實現協議,加代碼的地方在@interface那行的最後添加<UIActionSheetDelegate>,協議至關於java裏的接口,實現協議裏的方法。 spa

1@interface sheetviewViewController : UIViewController<UIActionSheetDelegate>
3 @end
二、添加button,命名button爲showSheetView.

三、爲button創建Action映射,映射到.h文件上,事件類型爲Action ,命名爲showSheet。 .net

四、在.m文件上添加點擊事件代碼 code

圖的效果是這樣的: 索引

01- (IBAction)showSheet:(id)sender {
02    UIActionSheet *actionSheet = [[UIActionSheet alloc]
03                                  initWithTitle:@"title,nil時不顯示"
04                                  delegate:self
05                                  cancelButtonTitle:@"取消"
06                                  destructiveButtonTitle:@"肯定"
07                                  otherButtonTitles:@"第一項", @"第二項",nil];
08    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
09    [actionSheet showInView:self.view];
10}

actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//設置樣式 接口

參數解釋:    事件

cancelButtonTitle  destructiveButtonTitle是系統自動的兩項。 開發

otherButtonTitles是本身定義的項,注意,最後一個參數要是nil。 get

[actionSheet showInView:self.view];這行語句的意思是在當前view顯示Action sheet。固然還能夠用其餘方法顯示Action sheet。

對應上面的圖和代碼,一目瞭然了把

五、接下來咱們怎麼相應Action Sheet的選項的事件呢?實現協議裏的方法。爲了能看出點擊Action sheet每一項的效果,咱們加入UIAlertView來作信息顯示。下面是封裝的一個方法,傳入對應的信息,在UIAlertView顯示對應的信息。

1-(void)showAlert:(NSString *)msg {
2    UIAlertView *alert = [[UIAlertView alloc]
3                          initWithTitle:@"Action Sheet選擇項"
4                          message:msg
5                          delegate:self
6                          cancelButtonTitle:@"肯定"
7                          otherButtonTitles: nil];
8    [alert show];
9}
那相應被Action Sheet選項執行的代碼以下:
01(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
02{
03    if (buttonIndex == 0) {
04        [self showAlert:@"肯定"];
05    }else if (buttonIndex == 1) {
06        [self showAlert:@"第一項"];
07    }else if(buttonIndex == 2) {
08        [self showAlert:@"第二項"];
09    }else if(buttonIndex == 3) {
10        [self showAlert:@"取消"];
11    }
12 
13}
14- (void)actionSheetCancel:(UIActionSheet *)actionSheet{ 
15 
16} 
17-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ 
18 
19} 
20-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{ 
21 
22}
能夠看到 buttonIndex 是對應的項的索引。

看到那個紅色的按鈕沒?那是ActionSheet支持的一種所謂的銷燬按鈕,對某戶的某個動做起到警示做用,

好比永久性刪除一條消息或圖像時。若是你指定了一個銷燬按鈕他就會以紅色高亮顯示:

actionSheet.destructiveButtonIndex=1;  

與導航欄相似,操做表單也支持三種風格 :

UIActionSheetStyleDefault              //默認風格:灰色背景上顯示白色文字   

UIActionSheetStyleBlackTranslucent     //透明黑色背景,白色文字   

UIActionSheetStyleBlackOpaque          //純黑背景,白色文字  

用法:

 actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//設置樣式

我選sheet 裏的第一項,顯示以下:

六、注意事項,在開發過程當中,發現有時候UIActionSheet的最後一項點擊失效,點最後一項的上半區域時有效,這是在特定狀況下才會發生,這個場景就是試用了UITabBar的時候纔有。解決辦法:

在showView時這樣使用,[actionSheet showInView:[UIApplication sharedApplication].keyWindow];或者[sheet showInView:[AppDelegate sharedDelegate].tabBarController.view];這樣就不會發生遮擋現象了。

相關文章
相關標籤/搜索