CustomActionSheet类继承UIActionSheet,具体的实现如下所示:
(1)CustomActionSheet.h头文件:
#import <UIKit/UIKit.h> @interface CustomActionSheet : UIActionSheet { UIToolbar* toolBar; UIView* view; } @property(nonatomic,retain)UIView* view; @property(nonatomic,retain)UIToolbar* toolBar; -(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title; @end
(2)CustomActionSheet.m实现文件:
#import "CustomActionSheet.h" @implementation CustomActionSheet @synthesize view; @synthesize toolBar; -(id)initWithHeight:(float)height WithSheetTitle:(NSString*)title{ self = [super init]; if (self) { int theight = height - 40; int btnnum = theight/50; for(int i=0; i<btnnum; i++){ [self addButtonWithTitle:@" "]; } toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; toolBar.barStyle = UIBarStyleBlackOpaque; UIBarButtonItem *titleButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:nil action:nil]; UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done)]; UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(docancel)]; UIBarButtonItem *fixedButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; NSArray *array = [[NSArray alloc] initWithObjects:leftButton,fixedButton,titleButton,fixedButton,rightButton,nil]; [toolBar setItems: array]; [titleButton release]; [leftButton release]; [rightButton release]; [fixedButton release]; [array release]; [self addSubview:toolBar]; view = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, height-44)]; view.backgroundColor = [UIColor groupTableViewBackgroundColor]; [self addSubview:view]; } return self; } -(void)done{ [self dismissWithClickedButtonIndex:0 animated:YES]; } -(void)docancel{ [self dismissWithClickedButtonIndex:0 animated:YES]; } -(void)dealloc{ [view release]; [super dealloc]; } @end
二、利用自定义的CustomActionSheet类显示提示框。
-(IBAction)doClick:(id)sender{ CustomActionSheet* sheet = [[CustomActionSheet alloc] initWithHeight:284.0f WithSheetTitle:@"自定义ActionSheet"]; UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0,50, 320, 50)]; label.text = @"这里是要自定义放的控制"; label.backgroundColor = [UIColor clearColor]; label.textAlignment = UITextAlignmentCenter; [sheet.view addSubview:label]; [sheet showInView:self.view]; [sheet release]; }
演示:
'프로그래밍 > iOS' 카테고리의 다른 글
MKMapview의 주석(Annotation)을 통해 상세뷰로 넘어 갈때, 인덱스 값 찾기. (0) | 2012.02.06 |
---|---|
동기적으로 동작하는 UIAlertView (Custom UIAlertView) (0) | 2012.02.05 |
아이폰 OS 개발 자료 총정리 (0) | 2012.02.04 |
XML 파싱을 쉽게 해주는 TBXML (0) | 2012.02.02 |
달력라이브러리 - TapkuLibrary (0) | 2012.02.01 |