显示为模态的两个导航控制器上的警告



我创建了两个UIViewControll,以模态形式呈现。比方说,对于前5次尝试,模态将正常出现,但之后它会给我一个:

Warning: Attempt to dismiss from view controller <UINavigationController: 0x76a8450> while a presentation or dismiss is in progress!

以下是解除当前视图控制器并显示另一个的代码

[customAlertLoad dismissViewControllerAnimated:NO completion:^{ 
     CustomAlertMsg *cust = [[CustomAlertMsg alloc] init];
     cust.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
     cust.modalPresentationStyle = UIModalPresentationFormSheet;
     cust.delegate = self;
     [self.navigationController presentModalViewController:cust animated:YES];
     cust.view.superview.frame = CGRectMake(0, 0, 458, 230);
     cust.view.superview.center = self.view.center;
}];

任何帮助都将不胜感激。

编辑

我刚刚想起几周前的另一个类似问题。你正在经历一个比赛状态,这可能可以使用GCD(大中央调度)来解决。这与@rakeshNS建议的解决方案类似,但危险性较小。(使用计时器处理竞争条件是一种糟糕的做法,尽管在许多语言中,调用计时器等待时间为0秒的方法是将该方法调用放在调用堆栈末尾的一种技巧,也就是使其异步)。苹果为此提供了一种机制。

我会尝试做这个轻微的编辑:

[customAlertLoad dismissViewControllerAnimated:NO completion:^{ 
                             dispatch_async(dispatch_get_main_queue(), ^(void) {
                                  CustomAlertMsg *cust = [[CustomAlertMsg alloc] init];
                                  cust.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
                                  cust.modalPresentationStyle = UIModalPresentationFormSheet;
                                  cust.delegate = self;
                                  [self.navigationController presentModalViewController:cust animated:YES];
                                  cust.view.superview.frame = CGRectMake(0, 0, 458, 230);
                                  cust.view.superview.center = self.view.center;
                                 });
                         }];

对dispatch_async的调用将块放在执行堆栈的末尾。在模态被驳回之前,这是不可能运行的。这也允许对两个模态的消除和呈现进行动画处理。

然而,我应该提到,在这个特殊的问题上,这是一个黑客您在其他地方有问题。如果我在测试项目中重现您的设置,我可以从完成块中创建/消除尽可能多的模态,而不会遇到错误(只要消除代码是从用于消除模态的自定义委托回调中调用的,就像苹果建议的那样)。我会查看您的自定义警报加载和自定义警报类代码。

结束编辑

这可能与您的问题不同,在将我的代码库转移到ARC后,我以前在Popover方面遇到过这个问题。我的Popover发布得太早或保留得太久,这会发生一些奇怪的事情。解决方案是创建一个实例变量来保存Popover,并手动管理它。然后一切都开始运转了。

所以我会这么做:

@interface YourClass:UIViewController
@property (nonatomic,strong) CustomAlertMsg *cust;
@end

@implementation YourClass
... Codey Code....
-(void)pushView{
     // For the sake of sanity, nil the modal here
     if(self.cust != nil) self.cust = nil;
     self.cust = [[CustomAlertMsg alloc] init];
     self.cust.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
     self.cust.modalPresentationStyle = UIModalPresentationFormSheet;
     self.cust.delegate = self;
     [self.navigationController presentModalViewController:self.cust animated:YES];
     self.cust.view.superview.frame = CGRectMake(0, 0, 458, 230);
     self.cust.view.superview.center = self.view.center;
}
// Then where you dismiss it, or in the delegate callback that is 
//  called when you dismiss it, if you are using one anyway, 
//  nil it out
- (void)methodThatDismissesModal
{
     [self dismissModalViewControllerAnimated:YES];
     // This is pretty important
     if(self.cust != nil) self.cust = nil; 
}
... Codey Code....
@end

正如错误消息所说,问题出现在解雇期间。所以不是presentModalViewController引起了你的麻烦,而是dismissViewControllerAnimated

如果您可以为该调用粘贴一些上下文,我们可能会找到解决方案。

尝试更改为:

[self.navigationController presentModalViewController:cust animated:NO];

也许可以尝试从导航控制器而不是customAlertLoad:中排除

[self.navigationController dismissViewControllerAnimated:NO completion:^{
    CustomAlertMsg *cust = [[CustomAlertMsg alloc] init];
    cust.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    cust.modalPresentationStyle = UIModalPresentationFormSheet;
    cust.delegate = self;
    [self.navigationController presentModalViewController:cust animated:YES];
    cust.view.superview.frame = CGRectMake(0, 0, 458, 230);
    cust.view.superview.center = self.view.center;
}];

试试这个,

[customAlertLoad dismissViewControllerAnimated:NO completion:^{ 
     [self performSelector:@selector(pushView) withObject:ni afterDelay:0.4];
}

-(void)pushView{
     CustomAlertMsg *cust = [[CustomAlertMsg alloc] init];
     cust.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
     cust.modalPresentationStyle = UIModalPresentationFormSheet;
     cust.delegate = self;
     [self.navigationController presentModalViewController:cust animated:YES];
     cust.view.superview.frame = CGRectMake(0, 0, 458, 230);
     cust.view.superview.center = self.view.center;
}

最新更新