UIAlertView、 UIActionSheet

一、UIAlertView、 UIActionSheet都是ios系统自带的弹出式对话框,当UIAlertView或UIActionSheet弹出来时用户无法与应用界面中的其它控件交互,UIAlertView与AUIctionSheet最大的区别在于UIAlertView是表现为显示在屏幕中央的弹出式警告框,而 ActionSheet表现为显示在屏幕底部的按钮列表。

1、创建UIAlertView。创建该对象时可致定改警告框的标题、消息内容、以及该警告框包含几个按钮等信息。如果程序需要监听用户点击饿警告框时那一个按钮,则需要在创建该UIAlertView时设置UIAlertViewDelegate委托对象。

2、调用UIAlertView显示。

3、如果需要监听用户点击了警告狂的那个按钮,则需为UIAlertViewDelegate协议中的方法。

二、UIActionSheet

UIActionSheet表现为显示在底部的按钮列表,用户通过单击某个按钮来表明自己的态度。默认情况下UIActionSheet只支持一个标题和多个按钮,UIActionSheet会有两个固定的按钮和多个其它按钮。

1、其创建与UIAlertView基本一致

三、具体代码如下:(包含相关的属性)

UIAlertView:警告框、窗口
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"提示内容"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:@"取消",@"按钮1",@"按钮2", nil];//alert创建   
    alert.title = @"标题";//设置标题
    alert.message = @"消息提示";//设置内容
   
    [alert show];
    //[alert dismissWithClickedButtonIndex:2 animated:YES];//消失alert
//触发事件
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%ld",(long)buttonIndex);
}
 
ActionSheet:选择器型态
添加 UIButton
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor grayColor];
    btn.frame = CGRectMake(10, 20, 80, 80);
        [btn addTarget:self action:@selector(showAlert) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
 
    添加 UITextField
    UITextField *sexField = [[UITextField alloc] initWithFrame:CGRectMake(10, 120, 200, 30)];
    sexField.borderStyle = UITextBorderStyleRoundedRect;
    sexField.delegate = self;//添加代理
    sexField.tag = 1;
    [self.view addSubview:sexField];
//触发事件
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField.tag == 1) {
       // [self showAlert];
        UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"男",@"女", nil];
        action.tag = 3;
        [action showInView:self.view];

        [textField resignFirstResponder];
    }
}

-(void)showAlert{
    UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"建行",@"农行", nil];
    action.tag = 4;
    [action showInView:self.view];

}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"=====%zi",buttonIndex);
    NSLog(@"---->%@",[actionSheet buttonTitleAtIndex:buttonIndex]);
   
    if (actionSheet.tag == 3) {
        UITextField  *field = (UITextField *)[self.view  viewWithTag:1];
        field.text = [actionSheet buttonTitleAtIndex:buttonIndex];
    }
}
 
原文地址:https://www.cnblogs.com/wxzboke/p/4972476.html