问题说明
在项目中, 有这样一个问题: 在A控制器中, 弹出一个alertView, 选择确定按钮, 在- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
代理方法中, 执行[UIApplication sharedApplication].keyWindow.rootViewController = BViewColtroller;
代码切换到B控制器.
结果出现了一个问题: B控制器一闪而过, 又回到了A控制器. 但是B控制器中的定时器还在运行, 并在定时结束后执行了B控制器中的相应代码.
demo说明
ViewController控制器有多个按钮可以通过多种方式跳转到NewViewController控制器, NewViewController控制器viewDidLoad方法中添加定时器, 5s后跳转到FrontPageViewController控制器.
//
// ViewController.m
// 关于坑爹的AlertView
//
// Created by wangyatao on 17/1/6.
// Copyright © 2017年 sinovatech. All rights reserved.
//
#import "ViewController.h"
#import "NewViewController.h"
@interface ViewController ()<UIAlertViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, [UIScreen mainScreen].bounds.size.width, 50)];
[self.view addSubview:label];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"第一个页面!";
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(20, 200, [UIScreen mainScreen].bounds.size.width-40, 40)];
[button setTitle:@"UIAlertView->跳转" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UIButton *button2 = [[UIButton alloc]initWithFrame:CGRectMake(20, 240, [UIScreen mainScreen].bounds.size.width-40, 40)];
[button2 setTitle:@"UIAlertController->跳转" forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(button2Clicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
UIButton *button3 = [[UIButton alloc]initWithFrame:CGRectMake(20, 280, [UIScreen mainScreen].bounds.size.width-40, 40)];
[button3 setTitle:@"UIAlertView->延迟1s->跳转" forState:UIControlStateNormal];
[button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button3 addTarget:self action:@selector(button3Clicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button3];
UIButton *button4 = [[UIButton alloc]initWithFrame:CGRectMake(20, 320, [UIScreen mainScreen].bounds.size.width-40, 40)];
[button4 setTitle:@"UIAlertView->alertView didDismiss代理中执行跳转" forState:UIControlStateNormal];
[button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button4 addTarget:self action:@selector(button4Clicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button4];
}
-(void)buttonClicked{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否跳转?" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
av.tag = 100;
[av show];
}
-(void)button2Clicked{
[self alertMessage:@"是否跳转?" andTitle:@"确定"];
}
-(void)button3Clicked{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否跳转?" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
av.tag = 200;
[av show];
}
-(void)button4Clicked{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否跳转?" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
av.tag = 300;
[av show];
}
//弹出提示信息
-(void)alertMessage:(NSString *)message andTitle:(NSString *)title{
UIAlertView *av ;
[av show];
UIAlertController *ac = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *aa = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NewViewController *adver =[[NewViewController alloc] init];
[UIApplication sharedApplication].keyWindow.rootViewController =adver;
}];
[ac addAction:aa];
[self presentViewController:ac animated:YES completion:nil];
}
#pragma mark - UIAlertViewDelegate
#pragma mark 点击按钮时执行
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == 100) {
//立即执行
NSLog(@"clickedButtonAtIndex:");
NewViewController *adver =[[NewViewController alloc] init];
[UIApplication sharedApplication].keyWindow.rootViewController = adver;
}else if(alertView.tag == 200){
//延迟1S执行
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NewViewController *adver =[[NewViewController alloc] init];
[UIApplication sharedApplication].keyWindow.rootViewController = adver;
});
}
}
#pragma mark alertView将要弹出
- (void)willPresentAlertView:(UIAlertView *)alertView {
NSLog(@"willPresentAlertView:");
}
#pragma mark alertView弹出
- (void)didPresentAlertView:(UIAlertView *)alertView {
NSLog(@"didPresentAlertView:");
}
#pragma mark alertView将要消失
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(@"willDismissWithButtonIndex:%ld",buttonIndex);
}
#pragma mark alertView消失
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(@"didDismissWithButtonIndex:%ld",buttonIndex);
if (alertView.tag == 300) {
NewViewController *adver =[[NewViewController alloc] init];
[UIApplication sharedApplication].keyWindow.rootViewController = adver;
}
}
@end
页面.png
各个跳转按钮的执行结果
按钮: UIAlertView->跳转
结果: 出现问题.
按钮: UIAlertController->跳转
结果: 未出现问题.
按钮: 延迟1s->跳转
结果: 未出现问题.
按钮: 在alertView didDismiss代理中执行跳转
结果: 未出现问题.
对于这个问题的猜想
A控制器弹出alertView,然后点击确认按钮时, 在- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
代理方法中, 执行[UIApplication sharedApplication].keyWindow.rootViewController = BViewColtroller;
这时候keyWindow切换了根控制器, B控制器显示出来, 然后alertView 消失. 在消失时,又将A控制器放在了根控制器的问题, 所以B控制器消失, 又显示了A控制器.
由此得到解决这个问题的方法:
方法一:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
代理方法执行时,添加延时,延时1s即可,这样可以等到alertView消失之后再执行[UIApplication sharedApplication].keyWindow.rootViewController = BViewColtroller;
. 从而避开这个问题.
方法二:
后来才发现, UIAlertViewDelegate代理中, 已经提供了代理方法:- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
, 这个代理方法会在alertView 消失后执行, 在这个方法中执行[UIApplication sharedApplication].keyWindow.rootViewController = BViewColtroller;
. 也可以避开这个问题.
方法三:
UIAlertView是个View, 我们可以使用UIAlertController来做弹出框,因为UIAlertController 是个controller, 所以也能避免这个问题.
网友评论