- #import "ActionSheetTestViewController.h"
- @implementation ActionSheetTestViewController
- /*
- Tasks
- Creating Action Sheets
- – initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles:
- Setting Properties
- delegate property
- title property
- visible property
- actionSheetStyle property 无例
- Configuring Buttons
- – addButtonWithTitle:
- numberOfButtons property
- – buttonTitleAtIndex:
- cancelButtonIndex property
- destructiveButtonIndex property
- firstOtherButtonIndex property
- Displaying
- – showFromTabBar:
- – showFromToolbar:
- – showInView:
- Dismissing
- – dismissWithClickedButtonIndex:animated:
- */
- // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- - (void)viewDidLoad {
- UILabel *numOfBtn = [[UILabel alloc]initWithFrame:CGRectMake(10.0, 10.0, 30.0, 30.0)];
- UILabel *titleOfBtn = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 10.0, 100.0, 30.0)];
- UILabel *cancelBtnIndex = [[UILabel alloc]initWithFrame:CGRectMake(200.0, 10.0, 30.0, 30.0)];
- UILabel *destructiveBtnIndex = [[UILabel alloc]initWithFrame:CGRectMake(10.0, 50.0, 30.0, 30.0)];
- UILabel *firstOtherBtnIndex = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 50.0, 30.0, 30.0)];
- UIActionSheet *actionSheetTest = [[UIActionSheet alloc]initWithTitle:@"ActionSheetTest"
- delegate:self
- cancelButtonTitle:@"CancelButton"
- destructiveButtonTitle:@"RedButton"
- otherButtonTitles:@"OtherButton1",@"OtherButton2",nil];
- //看actionSheet是否可见,这是一个只读属性
- BOOL a = actionSheetTest.visible;
- NSLog(@"%d",a);
- //不考虑指定索引的按钮的动作,可以设置是否有动画
- [actionSheetTest dismissWithClickedButtonIndex:0 animated:NO];
- //设置标题
- actionSheetTest.title = @"ActionSheetTitle";
- //通过给定标题添加按钮
- [actionSheetTest addButtonWithTitle:@"addButtonWithTitle"];
- //按钮总数
- numOfBtn.text = [NSString stringWithFormat:@"%d",actionSheetTest.numberOfButtons];
- //获取指定索引的标题
- titleOfBtn.text = [actionSheetTest buttonTitleAtIndex:4];
- //获取取消按钮的索引
- cancelBtnIndex.text = [NSString stringWithFormat:@"%d",actionSheetTest.cancelButtonIndex];
- //获取红色按钮的索引
- destructiveBtnIndex.text = [NSString stringWithFormat:@"%d",actionSheetTest.destructiveButtonIndex];
- //获取第一个其他按钮的索引
- firstOtherBtnIndex.text = [NSString stringWithFormat:@"%d",actionSheetTest.firstOtherButtonIndex];
- //设置actionSheet出现的方式
- [actionSheetTest showInView:self.view];//or [actionSheetTest showFromTabBar:] or [actionSheetTest showFromToolBar:]
- [self.view addSubview:numOfBtn];
- [self.view addSubview:titleOfBtn];
- [self.view addSubview:cancelBtnIndex];
- [self.view addSubview:destructiveBtnIndex];
- [self.view addSubview:firstOtherBtnIndex];
- [actionSheetTest release];
- [numOfBtn release];
- [titleOfBtn release];
- [cancelBtnIndex release];
- [destructiveBtnIndex release];
- [firstOtherBtnIndex release];
- [super viewDidLoad];
- }
- /*
- // Override to allow orientations other than the default portrait orientation.
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- */
- - (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- - (void)viewDidUnload {
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
- - (void)dealloc {
- [super dealloc];
- }
- #pragma mark -- UIActionSheetDelegate --
- //根据被点击按钮的索引处理点击事件
- - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
- NSLog(@"clickedButtonAtIndex:%d",buttonIndex);
- }
- //ActionSheet已经消失时
- - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
- NSLog(@"didDismissWithButtonIndex:%d",buttonIndex);
- }
- //ActionSheet即将消失时
- - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
- NSLog(@"willDismissWithButtonIndex:%d",buttonIndex);
- }
- //
- - (void)actionSheetCancel:(UIActionSheet *)actionSheet {
- NSLog(@"actionSheetCancel");
- }
- //ActionSheet已经显示时
- - (void)didPresentActionSheet:(UIActionSheet *)actionSheet {
- NSLog(@"didPresentActionSheet%@",actionSheet);
- }
- //ActionSheet即将显示时
- - (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
- NSLog(@"willPresentActionSheet%@",actionSheet);
- }
- @end
本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1208757,如需转载请自行联系原作者