2017-04-05 30 views
2

时调用dismissViewControllerAnimated方法,我得到了与理解问题,iOS的视图控制器和报警控制器如何在特定的情况下工作:UINavigation收盘UIAlertController

我有一个自定义UINavigationController,其中有我的UIViewController。我的导航控制器已覆盖dismissViewControllerAnimated:completion方法。从这个UIViewController我提出了新的UIAlertController。直到用户点击Alert中的任何按钮,一切正常。然而,奇怪的部分是,我的自定义的UINavigationController的dismissViewControllerAnimated:completion方法被调用(我不希望出现这种情况,如果可能的话...)

警报呈现有规则地(从UINavigationController的内部的UIViewController):

UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"yep" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 
    [self takeOrder:data]; 
}]; 

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"nope" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 

}]; 

[confirmOrderAcceptAlert addAction:okAction]; 
[confirmOrderAcceptAlert addAction:cancelAction]; 

[self presentViewController:alert animated:YES completion:nil]; 

是否有任何防止此行为的选项?为什么这首先发生?

编辑: 为dismissViewControllerAnimated:completion代码:

- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion { 
    self.isHeroEnabled = NO; 
    [super dismissViewControllerAnimated:flag completion:completion]; 
} 

我使用Hero库动画过渡,可能是这种情况?

+0

请出示dismissViewControllerAnimated的代码:完成您的自定义navigationcontroller –

+0

感谢您的答复,问题编辑 – Noiseapps

回答

2

由于您是UINavigationController的子类,因此它肯定会调用dismissViewControllerAnimated:completion。

为避免干扰库代码,请检查特定的ViewController类型。

如:

- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion { 
    if(![self.visibleViewController isKindOfClass:[UIAlertController class]]){ 
      self.isHeroEnabled = NO; 
    } 
    [super dismissViewControllerAnimated:flag completion:completion]; 
} 
+0

谢谢:)看来它的工作。然而,我不明白为什么要调用这个方法。我知道,“这就是它如何在ios中工作”,但为什么? :) – Noiseapps

+0

好吧!告诉我为什么你首先创建一个子类?要求是什么? :答案是当你想访问一个方法时,对所有的子类都是通用的,或者在父类中设置一个值,这个值将会反映在所有的子类中。在你的情况下,英雄库可能会设置一些iVar来识别演示视图控制器或弹出的视图控制器(不确定值是否是可用的)。 ;) –

+0

而且它调用这个方法的原因是因为alertviewcontroller被解雇了。苹果库会调用dismissviewcontroller方法,而你点击任何一个alertview按钮,它将调用uinavigationcontroller的这个方法。 –

0

这就是UINavigationController如何工作

如果你不想设置HeroEnabled名为由于警报的动作。您可能需要做一些像

if(![self.visibleViewController isKindOfClass:[UIAlertController class]]) { self.isHeroEnabled = NO }