UIActionSheet是iOS開發中實現警告框的重要的類,在很多情況下都要用到,先來一睹其芳容:
實現步驟如下:
一、爲了讓控制器類充當操作表的委託,控制器類需要遵從UIActionSheetDelegate協議。
@interface UIActionSheetDemoViewController : UIViewController <UIActionSheetDelegate>{
}
二、生成UIActionSheet並顯示。
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Are you sure?"
delegate:self
cancelButtonTitle:@"No way!"
destructiveButtonTitle:@"Yes, I'm sure."
otherButtonTitles:@"Button One", @"Button Two", nil];
[actionSheet showInView:self.view];
[actionSheet release];
三、點擊按鈕後的事件。
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"%i", buttonIndex);
if (buttonIndex == actionSheet.cancelButtonIndex) {
return;
}
switch (buttonIndex) {
case 0: {
NSLog(@"Item 1 Selected");
break;
}
case 1: {
NSLog(@"Item 2 Selected");
break;
}
case 2: {
NSLog(@"Item 3 Selected");
break;
}
}
}
代碼樣例請下載:http://download.csdn.net/detail/flyter/4274695
下面說說如何動態地添加UIActionSheet按鈕。
一、UIActionSheet的通常實現方法:
- (void)testActionSheetStatic {
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Static UIActionSheet"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Item A", @"Item B", @"Item C", nil];
[sheet showFromRect:view.bounds inView:view animated:YES];
[sheet release];
}
二、 如果事先知道各個按鈕並且再也不會改變的情況下,這樣的實現是OK的。但如果我要在運行時改變應該怎麼辦呢?動態添加按鈕看起來應該也很簡單,不要init函數中指定而在之後添加可以了,如下代碼就展示了這點。
- (void)testActionSheetDynamic {
// 創建時僅指定取消按鈕
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Dynamic UIActionSheet"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
// 逐個添加按鈕(比如可以是數組循環)
[sheet addButtonWithTitle:@"Item A"];
[sheet addButtonWithTitle:@"Item B"];
[sheet addButtonWithTitle:@"Item C"];
[sheet showFromRect:view.bounds inView:view animated:YES];
[sheet release];
}
運行下就發現問題很明顯——取消按鈕是在視圖的頂部,而標準做法是顯示在底部。怎麼解決呢?如果在init函數中添加取消按鈕就無法解決了。最後找到了一種將取消按鈕也動態添加。
- (void)testActionSheetDynamic {
// 創建時不指定按鈕
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Dynamic UIActionSheet"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// 逐個添加按鈕(比如可以是數組循環)
[sheet addButtonWithTitle:@"Item A"];
[sheet addButtonWithTitle:@"Item B"];
[sheet addButtonWithTitle:@"Item C"];
// 同時添加一個取消按鈕
[sheet addButtonWithTitle:@"Cancel"];
// 將取消按鈕的index設置成我們剛添加的那個按鈕,這樣在delegate中就可以知道是那個按鈕
sheet.cancelButtonIndex = sheet.numberOfButtons-1;
[sheet showFromRect:view.bounds inView:view animated:YES];
[sheet release];
}
這樣取消按鈕就顯示在底部並且行爲也符合預期了。
對我來說現在剩下的最大一個疑問就是destructive按鈕到底是什麼(Apple文檔也沒有清晰地說明這點)?一些實驗結果也表明它實際上和 取消按鈕並無區別,只不過它有一個紅色背景而不是黑色的。所有如果在上例中改變destructiveButtonIndex而不是 cancelButtonIndex,就可以看到標有“取消”的按鈕有紅色背景了。
三、出於完整性的考慮,和上述代碼相匹配的delegate代碼如下:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == actionSheet.cancelButtonIndex) {
return;
}
switch (buttonIndex)
{
case 0: {
NSLog(@"Item A Selected");
break;
}
case 1: {
NSLog(@"Item B Selected");
break;
}
case 2: {
NSLog(@"Item C Selected");
break;
}
}
}
最後說說如何自定義一個UIActionSheet類。
一、自定義CustomActionSheet類。
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];
}
演示:
代碼樣例請下載:http://download.csdn.net/detail/flyter/4274763