在我们的iOS开发中,常会见到如下界面的需求:
。
也就是点击按钮,出现选择提示框,我们今天使用两种方式(ActionSheet和AlertController)来实现该功能。示例代码上传至: https://github.com/chenyufeng1991/iOS-ActionSheet 。
【使用ActionSheet实现】
(1)实现代码如下:
#import "ViewController.h"
@interface ViewController ()<UIActionSheetDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - 按钮点击事件
- (IBAction)actionSheetButtonPressed:(id)sender {
/**
UIActionSheet已经在8.3后被弃用了,如果想要去掉警告信息,可以把项目的Deployment Target设置为8.3以下,就可以去掉警告了。
*/
/**
Title:如果不想要title,可以设置为nil;
注意需要实现UIActionSheetDelegate;
destructiveButtonTitle:设置的按钮文字是红色的;
otherButtonTitles:按照按钮顺序;
*/
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"这是标题" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"体育",@"娱乐", nil];
/**
*
UIActionSheetStyleAutomatic
UIActionSheetStyleDefaul