一、UIPopoverPresentationController
之前系统自带的UIPopoverController是专门为iPad服务的弹框视图,UIPopoverPresentationController是iOS8以后新增的,是UIViewController的属性。使用的的时候,需要创建的是UIViewController。
20160905172221309.png
//初始化内容视图控制器
MyViewController *contentVC = [[MyViewController alloc]init];
//设置大小
contentVC.preferredContentSize = CGSizeMake(110, 160);
//设置内容控制器模态显示类型为模态弹出窗
contentVC.modalPresentationStyle = UIModalPresentationPopover;
//初始化一个popover
self.popover = contentVC.popoverPresentationController;
//代理一定要
self.popover.delegate = self;
//设置弹出视图的颜色
self.popover.backgroundColor = [UIColor greenColor];
//设置popover的来源按钮(以button谁为参照)
self.popover.sourceView =button;
//设置弹出视图的位置(以button谁为参照)
self.popover.sourceRect = button.bounds;
//箭头的方向 设置成UIPopoverArrowDirectionAny 会自动转换方向
self.popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
//模态出弹框
[self presentViewController:contentVC animated:YES completion:nil];//推出popover
pragma mark -- 实现代理方法
//默认返回的是覆盖整个屏幕,需设置成UIModalPresentationNone。
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
return UIModalPresentationNone;
}
//点击蒙版是否消失,默认为yes;
-(BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
return YES;
}
//弹框消失时调用的方法
-(void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
NSLog(@"弹框已经消失");
}
二、UIAlertController
UIAlertView在IOS 8以上版本已经过时了,官方推荐我们使用UIAlertController代替UIAlertView、UIActionSheet
-
1、UIAlertController显示普通的Alert
20161226165508246.png
//显示提示框
//过时
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
// [alert show];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//响应事件
NSLog(@"action = %@", action);
}];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//响应事件
NSLog(@"action = %@", action);
}];
[alert addAction:defaultAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];
-
2、UIAlertController显示带文本输入的的Alert
20161226165520818.png
//提示框添加文本输入框
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//响应事件
//得到文本信息
for(UITextField *text in alert.textFields){
NSLog(@"text = %@", text.text);
}
}];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
//响应事件
NSLog(@"action = %@", alert.textFields);
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"登录";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
}];
[alert addAction:okAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];
-
3、UIAlertController显示ActionSheet
20161226165531740.png
//显示弹出框列表选择
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title"
message:@"This is an Sheet."
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
//响应事件
NSLog(@"action = %@", action);
}];
UIAlertAction* deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action) {
//响应事件
NSLog(@"action = %@", action);
}];
UIAlertAction* saveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//响应事件
NSLog(@"action = %@", action);
}];
[alert addAction:saveAction];
[alert addAction:cancelAction];
[alert addAction:deleteAction];
[self presentViewController:alert animated:YES completion:nil];
https://blog.csdn.net/shuai_summer/article/details/52441965
https://blog.csdn.net/weixin_37569048/article/details/80349155
网友评论