在IOS的用户接口向导中,苹果提供了另外一种显示警告框的手法,叫做UIActionSheet.它和UIAlertView比起来不会显得过于急切和紧张。而是很温和地在继续流程之前给用户提供了诸多选择。

1.普通的sheet框使用

同UIAlertView一样,sheet也可以很简单的创建并且显示.

 - (IBAction)actionSheetShow:(id)sender {
//destructiveButton 的颜色和其他按钮不同,呈现红色
//表示用户需要注意,一般用于不可逆操作
UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@"Hello" delegate:self cancelButtonTitle:@"OK" destructiveButtonTitle:@"此项需要用户注意" otherButtonTitles: nil];
[sheet showInView:self.view];
}

注意:如果我们开发了一款基于多标签页的程序(UITabbar),在每个标签页中显示sheet框的方法会发生不同。这时要调用“showFromTabBar”的方式来代替“showInView:aView”,否则界面的显示层次会发生问题。同样的,如果视图的底部有一条工具栏,我们可以调用“showFromToolbar”的方法来显示sheet框而不是“shwoInView:aView”.

如果显示的选择项过多,会出现滚动条选项在actionSheet

之后,同样可以使用代理方法获取用户的选择

 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
id buttonSheet=[actionSheet buttonTitleAtIndex:buttonIndex];
NSLog(@"按下了%@按钮",buttonSheet);
}

显示结果如下:

2.含进度条的sheet框

我们介绍进度条的sheet框,不显示任何按钮。同样借用模态的特性达到开发的某些目的。

 -(void)showProgressOnView:(UIView *)aView
{
if(!aView)
{
return;
} //sheet的创建
UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@"这时进度条\n\n\n" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil]; //进度条创建
UIProgressView *progress=[[UIProgressView alloc]initWithFrame:CGRectMake(0.0f, 40.0f, 220.0f, 90.0f)]; //进度条配置
progress.progressViewStyle = UIProgressViewStyleDefault;
progress.progress=0.0f;
[sheet addSubview:progress]; //起一个定时器,来更新进度条
[NSTimer scheduledTimerWithTimeInterval:0.03f target:self selector:@selector(updateProgress:) userInfo:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:progress,sheet, nil] forKeys:[NSArray arrayWithObjects:@"progress",@"sheet", nil]] repeats:YES]; [sheet showInView:self.view];
progress.center=CGPointMake(CGRectGetWidth(sheet.bounds)/, CGRectGetHeight(sheet.bounds)/);
}

NSTimer的响应函数如下:

 //NSTimer的响应函数如下
-(void)updateProgress:(NSTimer *)aTimer
{
UIProgressView *progress = nil;
UIActionSheet *sheet =nil; //将定时器中有用的内容取出来
NSDictionary *dicInfo =aTimer.userInfo;
if(!dicInfo)
{
return;
} progress = [dicInfo objectForKey:@"progress"];
if(!progress)
{
return;
} //进度条满了
if(progress.progress>=3.0f)
{
sheet = [dicInfo objectForKey:@"sheet"];
//将sheet关闭
if(sheet)
{
[sheet dismissWithClickedButtonIndex: animated:YES];
}
//定时器销毁
[aTimer invalidate]; }
else
{
//进度条的速度
progress.progress+=0.01f;
}
}

显示效果如下:

3.含自定义控件的sheet框

sheet框是一个UIView的子类,因而他不仅能够提供按钮选项,一定也支持往他身上加其他功能的控件。

这次让我们加一个取值器玩一玩,比如取值器中有5个待取的城市:上海,北京,深圳,香港,天津。具体实现代码如下:

 -(void)showCustomControlType:(UIView *)aView
{
//创建sheet,为picker腾出空间
UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"OK", nil];
sheet.actionSheetStyle=UIActionSheetStyleBlackOpaque; UIPickerView *picker=[[UIPickerView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 216.0f)];
picker.delegate=self;
picker.dataSource=self;
picker.showsSelectionIndicator=YES; [sheet addSubview:picker];
[sheet showInView:aView];
} -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (row) {
case :
return @"上海";
break;
case :
return @"北京";
break;
case :
return @"深圳";
break;
case :
return @"香港";
break;
case :
return @"天津";
break;
default:
return @"上海";
break;
}
} -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return ;
} -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return ;
}

运行结果如下:

IOS UIActionSheet的使用方法的更多相关文章

  1. iOS-提高iOS开发效率的方法和工具

    提高iOS开发效率的方法和工具 介绍 这篇文章主要是介绍一下我在iOS开发中使用到的一些可以提升开发效率的方法和工具. IDE 首先要说的肯定是IDE了,说到IDE,Xcode不能跑,当然你也可能同时 ...

  2. iOS中的过期方法和新的替代方法

    关于iOS中的过期方法和新的替代方法 1.获取某些类的UINavigationBar的统一外观并设置UINavigationbar的背景 注:方法名改了但是基本使用方法不变 + (instancety ...

  3. opencv直线检测在c#、Android和ios下的实现方法

    opencv直线检测在c#.Android和ios下的实现方法 本文为作者原创,未经允许,不得转载 :原文由作者发表在博客园:http://www.cnblogs.com/panxiaochun/p/ ...

  4. iOS开发——实用篇&提高iOS开发效率的方法和工具

    提高iOS开发效率的方法和工具 介绍 这篇文章主要是介绍一下我在iOS开发中使用到的一些可以提升开发效率的方法和工具. IDE 首先要说的肯定是IDE了,说到IDE,Xcode不能跑,当然你也可能同时 ...

  5. iOS与HTML5交互方法总结(转)

    今天小编在找技术文章的时候,发现这样一个标题:iOS与HTML5交互方法总结,怎么看着这么熟悉呢?   还以为是刚哥用了别的文章,点进去一看,原来是刚哥自己写的文章,他们转载的,而且还上了Dev St ...

  6. iOS UISearchController 的使用方法

    iOS UISearchController 的使用方法 UISearchController 让用户在 UISearchBar 上输入搜索关键词,展示搜索结果或者进行其他操作.UISearchCon ...

  7. IOS常见的加密方法,常用的MD5和Base64

    iOS代码加密常用加密方式 iOS代码加密常用加密方式,常见的iOS代码加密常用加密方式算法包括MD5加密.AES加密.BASE64加密,三大算法iOS代码加密是如何进行加密的,且看下文 MD5 iO ...

  8. iOS与HTML5交互方法总结(修正)

    摘要 看了不少别人写的博客或者论坛,关于iOS与HTML5交互方法大概主要有5种方式: 1. 利用WKWebView进行交互(系统API) 2. 利用UIWebView进行交互(系统API) 3. 苹 ...

  9. UIView封装动画--iOS利用系统提供方法来做转场动画

    UIView封装动画--iOS利用系统提供方法来做转场动画 UIViewAnimationOptions option; if (isNext) { option=UIViewAnimationOpt ...

随机推荐

  1. DataReader转泛型

    实体类的字段类型要和数据库一致,不然可能会出现错误. /// <summary> /// DataReader转泛型 /// </summary> /// <typepa ...

  2. C++中#include <xxx.h>和#include "xxx.h"的区别(尖括号和双引号的区别)

    第一种方式:采用< >方式,表示让编译器在编译器的预设标准路径下去搜索相应的头文件,如果找不到则报错. 例如:VS2015的安装目录\Microsoft Visual Studio 14. ...

  3. 弹出窗口a标签写下载,再弹出窗口

    如果这个窗口是弹出出口,直接<a href="">点击下载<a>是不行的,得用js这样写,弹出并关闭,不然会回到首页,如果没有定义首页会报错,<a h ...

  4. SQLite 终端相关命令

    SQLite ALL Last login: Fri Dec  5 09:52:08 on ttys002 BeSilent:~ qianfeng$ sqlite3 data.db SQLite ve ...

  5. T4模板之菜菜鸟篇

    一.废话 T4(Text Template Transformation Toolkit)是微软官方在VisualStudio 2008中开始使用的代码生成引擎.在 Visual Studio 中,“ ...

  6. js添加删除元素

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  7. C++ this 指针

    类的(非静态)成员函数具有一个附加的隐含形参,即指向该类对象的一个指针.这个隐含形参命名为this,与调用成员函数的对象绑定在一起.成员函数不能定义this形参,而是由编译器隐含地定义.成员函数的函数 ...

  8. css区分ie6,7,ff

    IE6能识别*,但不能识别 !important,IE7能识别*,也能识别!important;FF不能识别*,但能识别!important; 可以这样区别FF,IE7,IE6: background ...

  9. Java 遍历文件下jpg图片并解析图片

      package filetest; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; ...

  10. 使用Integer类实现二叉树排序

    class BinaryTree {     class Node {         private Comparable data;         private Node left;      ...