2017-04-05 28 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 }