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