什么是操作表單?看圖:
一看圖就明白了,毋需多說。
uiactionsheet* mysheet = [[uiactionsheet alloc]
initwithtitle:@"actionchoose"
delegate:self
cancelbuttontitle:@"cancel"
destructivebuttontitle:@"destroy"
otherbuttontitles:@"ok", nil];
[mysheet showinview:self.view];
與uialertview類似,我們也是在委托方法里處理按下按鈕后的動作。記得在所委托的類加上uiactionsheetdelegate。
- (void)actionsheetcancel:(uiactionsheet *)actionsheet{
//
}
- (void) actionsheet:(uiactionsheet *)actionsheet clickedbuttonatindex:(nsinteger)buttonindex{
//
}
-(void)actionsheet:(uiactionsheet *)actionsheet diddismisswithbuttonindex:(nsinteger)buttonindex{
//
}
-(void)actionsheet:(uiactionsheet *)actionsheet willdismisswithbuttonindex:(nsinteger)buttonindex{
//
}
看到那個紅色的按鈕沒?那是actionsheet支持的一種所謂的銷毀按鈕,對某戶的某個動作起到警示作用,
比如永久性刪除一條消息或者日志。如果你指定了一個銷毀按鈕他就會以紅色高亮顯示:
mysheet.destructivebuttonindex=1;
與導航欄類似,操作表單也支持三種風格 :
uiactionsheetstyledefault //默認風格:灰色背景上顯示白色文字
uiactionsheetstyleblacktranslucent //透明黑色背景,白色文字
uiactionsheetstyleblackopaque //純黑背景,白色文字
用法用例:
mysheet.actionsheetstyle = uiactionsheetstyleblackopaque;
常用方法和屬性
顯示actionsheet方法:
1.在一個視圖內部顯示,可以用showinview
[mysheet showinview:self];
2.如果要將actonsheet 與工具欄或者標簽欄對齊,可以使用showfromtoolbar或showfromtabbar
[mysheet showfromtoolbar:toolbar];
[mysheet showfromtabbar:tabbar];
解除操作表單
用戶按下按鈕之后,actionsheet就會消失——除非應用程序有特殊原因,需要用戶按下做個按鈕。用dismiss方法可令表單消失:
[mysheet dismisswithclickbuttonindex:1 animated:yes];
@property(nonatomic,copy) nsstring *title;
設置標題
@property(nonatomic) uiactionsheetstyle actionsheetstyle;
添加一個按鈕,會返回按鈕的索引
- (nsinteger)addbuttonwithtitle:(nsstring *)title;
[/code]
獲取按鈕標題
- (nsstring *)buttontitleatindex:(nsinteger)buttonindex;
獲取按鈕數量
@property(nonatomic,readonly) nsinteger numberofbuttons;
設置取消按鈕的索引值
@property(nonatomic) nsinteger cancelbuttonindex;
設置特殊標記
@property(nonatomic) nsinteger destructivebuttonindex;
視圖當前是否可見
@property(nonatomic,readonly,getter=isvisible) bool visible;
下面是幾種彈出方式,會根據風格不同展現不同的方式:
- (void)showfromtoolbar:(uitoolbar *)view;
- (void)showfromtabbar:(uitabbar *)view;
- (void)showfrombarbuttonitem:(uibarbuttonitem *)item animated:(bool)animated ;
- (void)showfromrect:(cgrect)rect inview:(uiview *)view animated:(bool)animated ;
- (void)showinview:(uiview *)view;
使用代碼將視圖收回
- (void)dismisswithclickedbuttonindex:(nsinteger)buttonindex animated:(bool)animated;
uiactionsheet代理方法
- (void)actionsheet:(uiactionsheet *)actionsheet clickedbuttonatindex:(nsinteger)buttonindex;
點擊按鈕時觸發的方法
- (void)willpresentactionsheet:(uiactionsheet *)actionsheet;
視圖將要彈出時觸發的方法
- (void)didpresentactionsheet:(uiactionsheet *)actionsheet;
視圖已經彈出式觸發的方法
- (void)actionsheet:(uiactionsheet *)actionsheet willdismisswithbuttonindex:(nsinteger)buttonindex;
點擊按鈕后,視圖將要收回時觸發的方法
- (void)actionsheet:(uiactionsheet *)actionsheet diddismisswithbuttonindex:(nsinteger)buttonindex;
點擊按鈕后,視圖已經收回時觸發的方法