UIAlerView、UIActionSheet 和UIAlertViewController(点击注销确认按钮实现)
1 - (IBAction)loginOut:(UIBarButtonItem *)sender { 2 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"确定要退出登陆吗" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定退出" otherButtonTitles:nil]; 3 [actionSheet showInView:self.view]; 4 } 5 6 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 7 if (buttonIndex == 0) { 8 //退出 9 [self.navigationController popViewControllerAnimated:YES]; 10 } 11 }
从IOS8之后,UIAlerView和UIActionSheet 两个合并在了一起,使用了一个新的控制器UIAlertViewController,好处就是不用实现代理
UIActionSheetDelegate和UIAlerViewDelegate
UIAlertViewController使用步骤如下:
//第一步,创建控制器 UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"确定退出吗" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; //第二步,创建按钮 UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"%s",__func__); }]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { [self.navigationController popViewControllerAnimated:YES]; }]; //第三步,添加按钮 [alertVC addAction:action1]; [alertVC addAction:action2]; //第四步,显示弹窗,相当于show操作 [self presentViewController:alertVC animated:YES completion:nil];