您的位置:首页 > 移动开发 > IOS开发

三、iOS8之后的弹窗指示器

2015-07-02 23:35 471 查看
在用户使用App进行一些危险性操作时,使用对话框的形式提示用户能起到很为用户着想的作用。经常使用的对话框有以下两种:UIActionSheet 和 UIAlertView。但在ios8之后 UIActionSheet 和 UIAlertView都定义为过时了,官方文档解释:

UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead

以下就是常用两种对话框的基本使用:

// 第一种方式

// 创建弹框指示器

UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"请确定要取消吗?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];

// 弹框提示

[sheet showInView:nil];

// 使用 UIActionSheet的代理方法进行相关操作

#pragma mark - actionSheet的代理方法

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;

{

if (buttonIndex == 0) {// 点击确定按钮

// 写上需要进行相关操作的代码

}

// 第二种方式

// 创建对话框

UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"亲,确定要注销吗?" preferredStyle:UIAlertControllerStyleActionSheet];

// 创建取消按钮

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

// 相关操作

}];

// 创建确定按钮

UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

// 回调方法

[self.navigationController popViewControllerAnimated:YES];

}];

// 添加按钮到对话框

[alert addAction:cancelAction];

[alert addAction:sureAction];

// 显示对话框

[self presentViewController:alert animated:YES completion:^{

// 完成显示后的相关操作

}];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: