1 //创建一个myAlert1操作表单对象(UIAlertControllerStyleActionSheet为操作表单,UIAlertControllerStyleAlert为警告视图) 2 UIAlertController *myAlert1 = [UIAlertController alertControllerWithTitle:@"Are you sure?" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 3 //创建一个操作表中的按钮对象(警告框、操作表默认没有按钮) 4 //参数handler参数是个块,具体实现点击这个按钮所需处理的内容 5 UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){ 6 7 NSString *msg; 8 if ([self.nameText.text length] > 0) { 9 msg = [NSString stringWithFormat:@"Your name is %@",self.nameText.text]; 10 11 }else{ 12 msg = @"You haven't input"; 13 } 14 15 UIAlertController *myAlert2 = [UIAlertController alertControllerWithTitle:@"Something Was Done" message:msg preferredStyle:UIAlertControllerStyleAlert]; 16 17 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Phew!" style:UIAlertActionStyleCancel handler:nil]; 18 //往myAlert2警告框对象中添加这个cancel按钮 19 [myAlert2 addAction:cancelAction]; 20 //在屏幕中绘制警告框对话框 21 [self presentViewController:myAlert2 animated:YES completion:nil]; 22 }]; 23 UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"No way!" style:UIAlertActionStyleCancel handler:nil]; 24 25 [myAlert1 addAction:yesAction]; 26 [myAlert1 addAction:noAction]; 27 28 //------这段代码开发iPhone app时可不写,iPad必须要写------- 29 UIPopoverPresentationController *ppc = myAlert1.popoverPresentationController; 30 31 if (ppc != nil) { 32 ppc.sourceView = sender; 33 ppc.sourceRect = sender.bounds; 34 //设置iPad操作表单的属性,使箭头方向为往下指 35 ppc.permittedArrowDirections = UIPopoverArrowDirectionDown; 36 } 37 //------这段代码开发iPhone app时可不写,iPad必须要写------- 38 39 [self presentViewController:myAlert1 animated:YES completion:nil];