iOS:AlerView、ActionSheet和AlertController的简单使用

来源:互联网 发布:弹幕网站源码 编辑:程序博客网 时间:2024/05/06 21:03

AlertView的使用:

AlerView的样式:
//alertView代理方法 监听确定按钮的点击- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    if (buttonIndex == 1) {        MCHeroModel *model = self.heroArr[alertView.tag];        model.name = [alertView textFieldAtIndex:0].text;        [self.tableView reloadData];    }}

同时AlertView还有代理方法可以监听按钮的点击

//点击cell显示alertView- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    //创建alertView对象    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"修改" message:@"修改名称" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];        //alerView的样式 有没有文本框、单独一个文本框、单独一个密码输入框和账号密码两个输入框的几种样式    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;    //拿到文本框并对其赋值    [alertView textFieldAtIndex:0].text = [self.heroArr[indexPath.row] name];    //显示    [alertView show];    alertView.tag = indexPath.row;}

ActionSheet的使用:

ActionSheet样式:

actionSheet的使用与alertView十分相似:
//点击cell显示actionSheet- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    //创建actionSheet对象    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"修改" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];    //actionSheet样式    sheet.actionSheetStyle = UIActionSheetStyleDefault;    //显示    [sheet showInView:self.view];    sheet.delegate = self;}
actionSheet也有代理方法:
//actionSheet代理方法- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{    if (buttonIndex == 0) {        NSLog(@"点击确定");    }}

AlertController的使用:

AlertView在iOS9.0过期,ActionSheet在iOS8.3也已经过期.
AlertController兼具两者的功能,是AlertView和ActionSheet的代替品.
AlertController没有代理方法,取而代之的是回调.
//点击cell显示alertController- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{     //创建AlertController对象 preferredStyle可以设置是AlertView样式或者ActionSheet样式    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"修改" message:@"修改信息" preferredStyle:UIAlertControllerStyleAlert];    //创建取消按钮    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {            }];    //创建确定按钮    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {        MCHeroModel *model = self.heroArr[indexPath.row];        model.name = alertC.textFields[0].text;        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];    }];    //添加文本框    [alertC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {        textField.text = [self.heroArr[indexPath.row] name];    }];    //添加按钮    [alertC addAction:action1];    [alertC addAction:action2];    //显示    [self presentViewController:alertC animated:YES completion:nil];}

0 0
原创粉丝点击