iOS 中UIAlertView 的简单使用

iOS 中UIAlertView 的简单使用

1.创建

1.普通样式

NSString *title = @"我是标题";  // 标题
    NSString *msg = @"你好";      // 你要显示的信息
    NSString *cancel = @"取消";   // 取消键的标题 点击后 alertView会消失
    NSString *other = @"其他:请点击";    // 其他按钮的标题,可以填写多个

   // 这里主要不要把nil丢了,这是结束标志 
   // 设置了代理为self后要遵守UIAlertViewDelegate协议
    UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:title
                                                       message:msg
                                                      delegate:self
                                             cancelButtonTitle:cancel
                                             otherButtonTitles:other, nil]; 
[alerView show]; // 这里是通过modal方式弹出

显示的样式就是这样了
这里写图片描述
如果需要添加多个选项按钮
可以通过创建

[alerView addButtonWithTitle:@"其他:1"];

这里添加
添加多个选项按钮后 样式也会改变
也可以通过如下方法添加多个选项按钮

UIAlertView的选项按钮尽量不要设置太多
一到两个既是理想
若想要设置更多的按钮可以通过UIActionSheet来实现

NSString *title = @"我是标题";  // 标题
    NSString *msg = @"你好";      // 你要显示的信息
    NSString *cancel = @"取消";   // 取消键的标题 点击后 alertView会消失
    NSString *other = @"其他:请点击";    // 其他按钮的标题,可以填写多个
    NSString *other1 = @"其他1:请点击";
    NSString *other2 = @"其他2:请点击";

    UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:title
                                                       message:msg
                                                      delegate:self
                                             cancelButtonTitle:cancel
                                             otherButtonTitles:other,other1,other2, nil];
    [alerView show];

这里写图片描述
如果添加按钮的标题过长

[alerView addButtonWithTitle:@"其他:过长过长过长过长过长过长过长过长过长过长过长过长过长过长过长过长"];

这里写图片描述

2.带输入框的样式
可以通过设置alertView的style设置样式

分别对应如下样式

alerView.alertViewStyle = UIAlertViewStylePlainTextInput;

UIAlertViewStylePlainTextInput这种是输入普通的文字
这里写图片描述

alerView.alertViewStyle =  UIAlertViewStyleSecureTextInput;

UIAlertViewStyleSecureTextInput这种是输入密码的样式
这里写图片描述

alerView.alertViewStyle =  UIAlertViewStyleLoginAndPasswordInput;

UIAlertViewStyleLoginAndPasswordInput这种是输入登陆账号密码的样式
这里写图片描述

2.代理方法

可以通过下面方法取出对应按钮执行方法,响应用户的操作

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"点击了第%zi个按钮",buttonIndex);
}

如果控制器控制多个UIAlertView,那么回调方法可以通过设置alertView的tag来区别不同的UIAlertView.
用switch语句来对不同的UIAlertView进行响应

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值