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

时间:2013-02-18 09:02:02

标签: ios uiviewcontroller

我创建了两个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;
}];

非常感谢任何帮助。

5 个答案:

答案 0 :(得分:2)

正如错误消息所示,问题出现在解雇期间。因此导致您遇到问题的不是presentModalViewController,而是dismissViewControllerAnimated

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

答案 1 :(得分:2)

修改

几周前我刚刚记得另一个类似的问题。您遇到了竞争条件,可以使用GCD(Grand Central Dispatch)解决。这类似于@rakeshNS建议的解决方案,但不那么危险。 (使用定时器是处理竞争条件的不良做法,尽管在许多语言中调用定时器等待0秒的方法是一种技巧,用于将该方法调用放在调用堆栈的末尾,也就是说它是异步的)。 Apple为此提供了一种机制。

我会尝试进行这种轻微的编辑:

[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,并手动管理它。然后一切都开始工作了。这是一个很好的工作。

所以我会这样做:

@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

答案 2 :(得分:1)

尝试更改为:

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

答案 3 :(得分:1)

也许尝试从导航控制器中解除而不是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;

}];

答案 4 :(得分:-1)

试试这个,

[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;
}