UIAlertView和UIActionSheet 分类: ios开发 ...

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;

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/xinxinit/p/4677750.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值