UIAlertController警告视图和操作表单

时间:2024-01-02 22:45:02
 //创建一个myAlert1操作表单对象(UIAlertControllerStyleActionSheet为操作表单,UIAlertControllerStyleAlert为警告视图)
UIAlertController *myAlert1 = [UIAlertController alertControllerWithTitle:@"Are you sure?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//创建一个操作表中的按钮对象(警告框、操作表默认没有按钮)
//参数handler参数是个块,具体实现点击这个按钮所需处理的内容
UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){ NSString *msg;
if ([self.nameText.text length] > ) {
msg = [NSString stringWithFormat:@"Your name is %@",self.nameText.text]; }else{
msg = @"You haven't input";
} UIAlertController *myAlert2 = [UIAlertController alertControllerWithTitle:@"Something Was Done" message:msg preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Phew!" style:UIAlertActionStyleCancel handler:nil];
//往myAlert2警告框对象中添加这个cancel按钮
[myAlert2 addAction:cancelAction];
//在屏幕中绘制警告框对话框
[self presentViewController:myAlert2 animated:YES completion:nil];
}];
UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"No way!" style:UIAlertActionStyleCancel handler:nil]; [myAlert1 addAction:yesAction];
[myAlert1 addAction:noAction]; //------这段代码开发iPhone app时可不写,iPad必须要写-------
UIPopoverPresentationController *ppc = myAlert1.popoverPresentationController; if (ppc != nil) {
ppc.sourceView = sender;
ppc.sourceRect = sender.bounds;
//设置iPad操作表单的属性,使箭头方向为往下指
ppc.permittedArrowDirections = UIPopoverArrowDirectionDown;
}
//------这段代码开发iPhone app时可不写,iPad必须要写------- [self presentViewController:myAlert1 animated:YES completion:nil];