UIAlertView和UIActionSheet 分类: ios开发 2015-04-09 20:43 152人阅读 评论(0) 收藏
2015-04-09 20:43
369 查看
UIAlertView和UIActionSheet是iOS自带的弹出式对话框。当这俩个控件出现时,用户无法与其他控件进行交互。
两个区别在于:
UIAlertView是显示在屏幕中央的,而UIActionSheet是显示在底部的按钮列表。
UIAlertView的用法非常简单:
1.创建UIAlertView,指定该对话框的标题、消息内容、以及该对话框包含的按钮信息。如果要监听按钮点击警告框的哪个按钮,需要设置UIAlertViewDelegate委托对象。
2.显示UIAlertView即可。
3.若要监听某按钮,则为委托对象实现UIAlertViewDelegate协议中的方法。
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@”提示”//指定标题
message:@”警告框使用起来很简单的啦!”//指定消息
delegate:self //指定委托对象
cancelButtonTitle:@”确定” //取消按钮设置标题
otherButtonTitles:@”取消”,//其他按钮
nil];
[alert show];
UIAlertViewDelegate协议中定义了几个的方法,但最常用的就是:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
当用户点击了某个按钮就会激发该方法,其中buttonIndex参数代表用户点击的按钮的索引,当然了,索引是从0开始。
使用方法如下:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{ NSString* msg = [[NSString alloc] initWithFormat:@”您按下的第%d个按钮!”,buttonIndex];
NSLog(@”%@”,msg);
}
在日常的项目开发中,可能要用到带输入框的UIAlerView,so easy!只需要增加一个属性:alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;
注意:
typedef enum style
{
UIAlertViewStyleDefault,//默认状态的警告框
UIAlertViewStyleSecureTextInput,//包括一个密码输入框
UIAlertViewStylePlainTextInput,//包括一个普通输入框
UIAlertViewStyleLoginAndPasswordInput//包括用户名、密码的输入框
}UIAlertView;
设置第二框为数字框:
[alert textFieldAtIndex:1].keyboardType=UIKeyboardTypeNumberPad;
实现一个代理函数:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *nameField=[alertView textFieldAtIndex:0];
UITextField *passField=[alertView textFieldAtIndex:1];
}
UIActionSheet的学习
有了上面的基础,UIActionSheet与上面的类似创建方式
UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@”提示”//指定标题
delegate:self //指定委托对象
cancelButtonTitle:@”取消”//指定取消按钮的标题
destructiveButtonTitle:@”确定”//指定销毁按钮的标题
otherButtonTitles:@”其他的说点什么好呢?”,//其他按钮设置标题
nil];
sheet.actionSheetStyle=UIAlertViewStyleDefault;
[sheet showInView:self.view];
当你点击了某个按钮会激发下面的方法:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//同样参数buttonIndex也为监听的按钮数
}
typedef enum style
{
UIActionSheetStyleDefault,//默认风格,灰色背景上显示白色的文字
UIActionSheetStyleBlackTranslucent,//在透明的黑色背景上显示白色的文字
UIActionSheetStyleBlackOpaque//在纯黑色的背景上显示白色的文字
两个区别在于:
UIAlertView是显示在屏幕中央的,而UIActionSheet是显示在底部的按钮列表。
UIAlertView的用法非常简单:
1.创建UIAlertView,指定该对话框的标题、消息内容、以及该对话框包含的按钮信息。如果要监听按钮点击警告框的哪个按钮,需要设置UIAlertViewDelegate委托对象。
2.显示UIAlertView即可。
3.若要监听某按钮,则为委托对象实现UIAlertViewDelegate协议中的方法。
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@”提示”//指定标题
message:@”警告框使用起来很简单的啦!”//指定消息
delegate:self //指定委托对象
cancelButtonTitle:@”确定” //取消按钮设置标题
otherButtonTitles:@”取消”,//其他按钮
nil];
[alert show];
UIAlertViewDelegate协议中定义了几个的方法,但最常用的就是:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
当用户点击了某个按钮就会激发该方法,其中buttonIndex参数代表用户点击的按钮的索引,当然了,索引是从0开始。
使用方法如下:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{ NSString* msg = [[NSString alloc] initWithFormat:@”您按下的第%d个按钮!”,buttonIndex];
NSLog(@”%@”,msg);
}
在日常的项目开发中,可能要用到带输入框的UIAlerView,so easy!只需要增加一个属性:alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;
注意:
typedef enum style
{
UIAlertViewStyleDefault,//默认状态的警告框
UIAlertViewStyleSecureTextInput,//包括一个密码输入框
UIAlertViewStylePlainTextInput,//包括一个普通输入框
UIAlertViewStyleLoginAndPasswordInput//包括用户名、密码的输入框
}UIAlertView;
设置第二框为数字框:
[alert textFieldAtIndex:1].keyboardType=UIKeyboardTypeNumberPad;
实现一个代理函数:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *nameField=[alertView textFieldAtIndex:0];
UITextField *passField=[alertView textFieldAtIndex:1];
}
UIActionSheet的学习
有了上面的基础,UIActionSheet与上面的类似创建方式
UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@”提示”//指定标题
delegate:self //指定委托对象
cancelButtonTitle:@”取消”//指定取消按钮的标题
destructiveButtonTitle:@”确定”//指定销毁按钮的标题
otherButtonTitles:@”其他的说点什么好呢?”,//其他按钮设置标题
nil];
sheet.actionSheetStyle=UIAlertViewStyleDefault;
[sheet showInView:self.view];
当你点击了某个按钮会激发下面的方法:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//同样参数buttonIndex也为监听的按钮数
}
typedef enum style
{
UIActionSheetStyleDefault,//默认风格,灰色背景上显示白色的文字
UIActionSheetStyleBlackTranslucent,//在透明的黑色背景上显示白色的文字
UIActionSheetStyleBlackOpaque//在纯黑色的背景上显示白色的文字
}UIActiosheet;
相关文章推荐
- UIPageControl+UIScrollView 分类: ios开发 2015-04-09 20:48 160人阅读 评论(0) 收藏
- 图灵社团ios培训第一周(类) 分类: ios开发 2015-03-28 20:39 152人阅读 评论(0) 收藏
- ios UIScrollView 循环滑动 分类: ios开发 2015-01-29 17:22 138人阅读 评论(0) 收藏
- iOS开发 调用系统相机和相册 分类: ios技术 2015-03-30 15:52 65人阅读 评论(0) 收藏
- 如何去掉NavigationBar的backButton 分类: ios开发 2015-01-26 11:39 323人阅读 评论(0) 收藏
- iOS开发- 以图搜图功能实现 (源码+解析) 分类: ios开发 2015-01-08 19:55 194人阅读 评论(0) 收藏
- iOS多线程编程之NSOperation和NSOperationQueue的使用 分类: ios开发 2015-01-01 12:34 145人阅读 评论(0) 收藏
- iOS网络编程实践--蓝牙对等网络通信实例讲解 分类: ios开发 2015-01-01 13:40 160人阅读 评论(0) 收藏
- 图灵ios培训第一周(使用UIWebView创建简易浏览器) 分类: ios开发 2015-03-28 12:43 295人阅读 评论(0) 收藏
- iphone获取屏幕尺寸以及状态栏 分类: ios开发 2015-03-29 21:35 198人阅读 评论(0) 收藏
- iOS开发网络篇—使用ASI框架进行文件下载 分类: ios开发 2015-02-02 10:43 133人阅读 评论(0) 收藏
- iOS开发网络篇—网络编程基础 分类: ios开发 2015-01-21 19:24 140人阅读 评论(0) 收藏
- iOS UIButton各类属性设置大全 分类: ios开发 2015-04-01 16:39 185人阅读 评论(0) 收藏
- ios 同步Get请求的实现 分类: ios开发 2015-01-30 17:36 362人阅读 评论(0) 收藏
- ios俩个APP之间跳转、传值 分类: ios开发 2015-02-01 14:41 370人阅读 评论(0) 收藏
- iOS开发网络篇—JSON介绍 分类: ios开发 2015-01-24 15:01 137人阅读 评论(0) 收藏
- iOS开发网络篇—搭建本地服务器 分类: ios开发 2015-01-21 19:05 257人阅读 评论(1) 收藏
- IOS导航栏的使用方法 分类: ios开发 2015-01-29 10:40 282人阅读 评论(0) 收藏
- iOS开发网络篇—HTTP协议 分类: ios开发 2015-01-21 19:45 147人阅读 评论(0) 收藏