标签:
我感觉UIActionSheet和UIAlertView的用法差不多,都很简单,下面给出一个简单的Demo,具体想用哪个,根据公司要求和个人爱好。
#import "ViewController.h"
@interface ViewController ()<UIActionSheetDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 100, 40);
button.backgroundColor = [UIColor orangeColor];
[button setTitle:@"弹出操作表" forState:UIControlStateNormal];
[button addTarget:self action:@selector(showActionSheet:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)showActionSheet:(UIButton*)button {
//destructiveButton是红色按钮,是ActionSheet支持的一种所谓的销毁按钮,对用户的某种行为起到警示作用,比如修改、删除某个东西
//destructiveButton,1,2,3,Cancel,它们的buttonIndex分别为0,1,2,3,4
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"呵呵" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"destructiveButton" otherButtonTitles:@"1",@"2",@"3", nil];
[actionSheet showInView:self.view];
}
// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (0 == buttonIndex) {
NSLog(@"0");
} else if (1 == buttonIndex) {
NSLog(@"1");
} else if (2 == buttonIndex) {
NSLog(@"2");
} else if (3 == buttonIndex) {
NSLog(@"3");
} else {
NSLog(@"4");
}
}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet{
NSLog(@"willPresentActionSheet");
}// before animation and showing view
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet{
NSLog(@"didPresentActionSheet");
}// after animation
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(@"willDismissWithButtonIndex");
}// before animation and hiding view
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(@"didDismissWithButtonIndex");
}// after animation
@end
标签:
原文地址:http://www.cnblogs.com/yyt-hehe-yyt/p/4741833.html