您的位置:首页 > 产品设计 > UI/UE

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

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