写在前边:
一个视图控制器仅能使用presentViewController模态方法弹出一个控制器;
如果想在模态方法弹出第二个视图控制器,可以使用已弹出的alert1来弹出.
UIAlertView 用法
- (void)showAlertView {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead, ios(2.0, 9.0)" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
[self presentViewController:alert animated:true completion:nil];
}
#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
printf("%ld",buttonIndex);
}
上面使用UIAlertView 需要使用代理处理自定义事件;
UIAlertController 用法
- (void)showAlert:(NSString *)title {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"提示%@",title] message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:confirm];
[self presentViewController:alert animated:true completion:nil];
}
与UIAlertView相比 直观的赶脚是代码量上减少了;
关于alert ,随着业务不断的递增,屏幕上展示的alert提示框个数和样式也在不断的增加,直接导致体验不友好,UIAlertController就是苹果为了规范提示框新增的API,但是这里需要注意,如果使用上面的- (void)showAlert:(NSString *)title
可能会出现和UIAlertView一样一次展示多个提示的问题;在网上参考了大家的分享,总结如下:
- (void)showAlert:(NSString *)title {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"提示%@",title] message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:confirm];
//两种方式不一样
//方式一:类似 UIAlertView 可以同时展示几个
[[LBAlertViewController topViewController] presentViewController:alert animated:true completion:nil];
//方式二:
// [self presentViewController:alert animated:true completion:nil];
}
+ (UIViewController *)getCurrentVC {
//1. 先找到KeyWindow
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
//2. 正常情况下KeyWindow应该是在UIWindowLevelNormal,有Alert的时候KeyWindow就是Alert框
if (window.windowLevel != UIWindowLevelNormal)
{
//3. 如果不是UIWindowLevelNormal,那么找到UIWindowLevelNormal级别的Window
// 这里有个缺陷,因为UIWindowLevelNormal的不一定只有一个,虽然正常情况下只有一个
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tmpWin in windows)
{
if (tmpWin.windowLevel == UIWindowLevelNormal)
{
//找到了UIWindowLevelNormal的Window
window = tmpWin;
break;
}
}
}
//4. 判断RootViewController不是TabBarVC和NaviVC,且是ViewController
id result = window.rootViewController;
BOOL isViewController = ![result isKindOfClass:[UITabBarController class]] && ![result isKindOfClass:[UINavigationController class]] && [result isKindOfClass:[UIViewController class]];
//5. 进入递归循环,排除TabBarVC和NaviVC,以及进入PresentedVC继续递归
while (!isViewController) {
while ([result isKindOfClass:[UITabBarController class]]) {
UITabBarController *tempVC = result;
result = [tempVC selectedViewController];
}
while ([result isKindOfClass:[UINavigationController class]]) {
UINavigationController *tempVC = result;
result = [tempVC.viewControllers lastObject];
}
//MARK: 重点是这里 如果上一个是present 则会直接返回上一个alert
id presentedVC = [result presentedViewController];
if (presentedVC) {
result = presentedVC;
}
//MARK:---- 注释上部分则会present一次
isViewController = ![result isKindOfClass:[UITabBarController class]] && ![result isKindOfClass:[UINavigationController class]] && [result isKindOfClass:[UIViewController class]];
}
return result;
}
注意理解开头提到的,就会明白怎么才会只提示一次alert!当然如果注释相关代码也会如UIAlertView一样alert满天飞!!!