自定義類的類名是CustomActionSheet。頭文件如下所示:
#導入& ltui kit/ui kit . h & gt;
@ interface CustomActionSheet:UIActionSheet
@property (nonatomic,retain)ui view * custom view;
@property (nonatomic,retain)ns string * custom title;
-(id)initWithViewHeight:(float)_ height with sheet title:(ns string *)_ title;
@end
解釋:自定義視圖是壹個可以自定義的區域。在使用我定制的類時,只需獲取customView,並為其添加Subview,非常方便。CustomTitle是頂欄的標題。這裏有壹個初始化方法。
-(ID)initwithviewheight:(float)_ heightwithSheettitle:(ns string *)_ Title _ Title賦給customTitle,而_ Height是自定義UIActionSheet中自定義區域的高度,對應上圖,就是UIDatePicker所占區域的高度。自定義區域的寬度為320,不需要設置。
然後是CustomActionSheet.m文件,核心代碼是重寫的layoutSubviews函數。代碼如下:
-(void)布局子視圖{
[超級布局子視圖];
//
//CGRect new frame = self . frame;
////new frame . origin . y = 459;
//new frame . origin . y = 459-customViewHeight-NavBarHeight;
//self . frame = new frame;
UINavigationBar * navBar =[[UINavigationBar alloc]initWithFrame:CGRectMake(0,view height-customViewHeight-NavBarHeight,320,NavBarHeight)];
navbar . bar style = UIBarStyleBlackOpaque;
UINavigationItem * navItem =[[UINavigationItem alloc]init with title:self . custom title];
uibarbuttoniitem * left button =[[uibarbuttoniitem alloc]initwithttitle:@ " Cancel " style:uibarbuttonitemstylebounded target:self action:@ selector(doc ancel)];
navitem . leftbarbuttonitem = left button;
uibarbuttoniitem * right button =[[uibarbuttoniitem alloc]initwithttitle:@ " OK " style:uibarbuttonitemstyledone target:self action:@ selector(done)];
navitem . rightbarbuttonitem = right button;
NSArray * array =[[NSArray alloc]initWithObjects:navItem,nil];
[navBar set items:array];
【self . superview add subview:navBar】;
【self . superview add subview:self . custom view】;
}
然後點擊按鈕後有兩個事件觸發功能。代碼如下:
-(無效)完成{
[self dismisswitchedbuttonindex:0 animated:YES];
[self . delegate action sheet:self clicked buttonatindex:0];
}
-(無效)文檔取消{
[self dismisswitchedbuttonindex:1 animated:YES];
[self . delegate action sheet:self clicked buttonatindex:1];
}
使用自定義控件的類需要實現UIActionSheetDelegate協議。功能:
-(void)動作表:(UIActionSheet *)動作表
單擊按鈕索引:(NSInteger)按鈕索引
點擊按鈕後做壹些事情。ButtonIndex值為0,點擊確定觸發,點擊取消觸發1。