码迷,mamicode.com
首页 > 其他好文 > 详细

UIAlertView和UIActionSheet

时间:2015-04-09 21:58:26      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:uialertvie   uiactionsh   警告框   消息   按钮   

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//在纯黑色的背景上显示白色的文字

}UIActiosheet;

UIAlertView和UIActionSheet

标签:uialertvie   uiactionsh   警告框   消息   按钮   

原文地址:http://blog.csdn.net/it_ds/article/details/44965193

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!