ActionSheet和AlertView比较相似,都是给用户一个提示信息。但是它是从底部弹出。它通常用于确认潜在的危险或不能撤销的操作。比如删除一条数据。我们使用ActionSheet时,需要在h文件中实现UIActionSheetDelegate协议。
通常我们需要实现:actionSheet:didDismissWithButtonIndex:该方法是ActionSheet消失的时候调用。
下面我写一个测试例子,先看效果图
源码:
头文件:
#import <UIKit/UIKit.h>
@interface Hello_WorldViewController : UIViewController
<UIActionSheetDelegate> {
UITextField *txtField;
}
@property (nonatomic, retain) IBOutlet UITextField *txtField;
-(IBAction)onClickButton:(id)sender;
@end
源文件:
@implementation Hello_WorldViewController
@synthesize txtField;
-(IBAction)onClickButton:(id)sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"您确认清除文本框中的数据吗?"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:@"确定"
otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet release];
}
-(void)actionSheet:(UIActionSheet *)actionSheet
didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == [actionSheet destructiveButtonIndex])
{
txtField.text = @"";
}
}