iOS开发——ActionSheet的使用与弹出选择对话框

来源:互联网 发布:美工区域材料的投放 编辑:程序博客网 时间:2024/05/28 01:35

     在我们的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   UIActionSheetStyleDefault   UIActionSheetStyleBlackTranslucent   UIActionSheetStyleBlackOpaque  */  //这里的actionSheetStyle也可以不设置;  actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;  [actionSheet showInView:self.view];}/** *  UIActionSheetDelegate中自动回调的方法; 响应事件在里面处理; */#pragma mark - UIActionSheetDelegate- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{//按照按钮的顺序0-N;  switch (buttonIndex) {    case 0:      NSLog(@"点击了确定");      break;    case 1:      NSLog(@"点击了体育");      break;    case 2:      NSLog(@"点击了娱乐");      break;    case 3:      NSLog(@"点击了取消");      break;          default:      break;  }  }@end

(2)由于我的项目是部署在iOS9上的(Deployment Target 为9.0),上述代码会报一个警告:


表示UIActionSheet已经在iOS8.3后被弃用了。推荐我们使用UIAlertController中的UIAlertControllerStyleActionSheet来替代。

但是处理这类警告(*** is deprecated:first deprecated in iOS ...)有一个投机取巧的方法,直接把我们的项目的部署目标(Deployment Target)设为被弃用的版本之前即可。如***已经在9.0中被弃用了,那么我们把Deployment Target设为8.x就不会报警告了,设为9.x的话就会报警告,只要低于开始弃用时的版本即可。


 (3)运行程序,上述的实现效果如下:



  

【使用AlertController实现】

既然苹果官方推荐我们使用AlertController来替换ActionSheet,那么我们同样使用AlertController来实现一下:关于AlertController的其他使用,请参考《iOS9使用提示框的正确实现方式》。

(1)代码实现如下:

#import "SecondViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)viewDidLoad {  [super viewDidLoad];}#pragma mark - 弹出选择提示框- (IBAction)buttonPressed:(id)sender {  //初始化提示框;  /**   preferredStyle参数:   UIAlertControllerStyleActionSheet,   UIAlertControllerStyleAlert   *  如果要实现ActionSheet的效果,这里的preferredStyle应该设置为UIAlertControllerStyleActionSheet,而不是UIAlertControllerStyleAlert;   */  UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:  UIAlertControllerStyleActionSheet];  /**   *  style参数:   UIAlertActionStyleDefault,   UIAlertActionStyleCancel,   UIAlertActionStyleDestructive(默认按钮文本是红色的)   *   */  //分别按顺序放入每个按钮;  [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {    //点击按钮的响应事件;    NSLog(@"点击了确定");  }]];  [alert addAction:[UIAlertAction actionWithTitle:@"体育" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {    //点击按钮的响应事件;    NSLog(@"点击了体育");  }]];  [alert addAction:[UIAlertAction actionWithTitle:@"娱乐" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {    //点击按钮的响应事件;    NSLog(@"点击了娱乐");  }]];  [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {    //点击按钮的响应事件;    NSLog(@"点击了取消");  }]];  //弹出提示框;  [self presentViewController:alert animated:true completion:nil];}#pragma mark - 返回按钮的点击- (IBAction)backPressed:(id)sender {  [self dismissViewControllerAnimated:true completion:nil];}@end

(2)运行效果如下:基本同样可以实现和ActionSheet相同的效果。




【ActionSheet和AlertController实现的比较】

比较上述两种实现方式,我们来看看它们有什么不同:

(1)使用ActionSheet实现时,点击提示框外的其他区域,相当于点击了“取消”按钮,提示框消失;而使用AlertController实现时,点击除提示框外的空白区域,界面没有任何响应。

(2)使用ActionSheet实现时,“取消”按钮和其他按钮之间有空白间隔;而使用AlertController实现时,所有按钮都是连在一起的。


    总结,大家可以根据自己的实际开发需求选择不同的实现方式。当然,如果学有余力,也可以进行控件的自定义。



github主页:https://github.com/chenyufeng1991  。欢迎大家访问!

最近极客学院Wiki正在进行IT职业技能图谱的制定,我主要负责iOS方向,大家感兴趣的可以一起参加,有问题或者修改可以直接给我发issues或者pull request。https://github.com/chenyufeng1991/skillmap  。


5 0
原创粉丝点击