2015-09-22 45 views
6

我的問題是,我如何獲得以下自定義展開segue以使用iOS 9以前版本的設備以及運行iOS 9的設備?自定義解除問題iOS 8和iOS 9

我有一個自定義塞格顯示視圖控制器,然後有一個相應的自定義解繞Segue。此代碼在iOS 8中運行良好,並通過創建UIStoryboardSegue的子類和實現perform方法來實現。然後,我重寫我的自定義導航控制器的以下方法:

- (UIStoryboardSegue *) segueForUnwindingToViewController: (UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier 
{ 
    UIStoryboardSegue *segue; 
    if([fromViewController isKindOfClass:[MyViewController class]]){ 
     segue = [[CustomSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController]; //Custom Unwind Segue 
    } 
    else{ 
     UIStoryboardSegue *unwindSegue = [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier]; //Normal Unwind Segue 
     segue = unwindSegue; 
    } 
    return segue; 
} 

在iOS系統中9,segueForUnwindingToViewController已被棄用。它仍然適用於MyViewController CustomSegue;然而,默認的展開順序不再適用於任何其他展開順序。雖然在超級方法上調用方法會返回一個展開順序,但從不會發生延遲,視圖控制器永遠不會彈出,並且用戶永遠不能返回到前一個屏幕。因此,爲了清楚起見,如果我使用常規show segue,則相應的unwind segue將調用不贊成使用的方法,該方法會在super上調用該方法,但不起作用。

我觀看了WWDC在故事板上的演講,並且我能夠在iOS 9中解決這個問題:a)不再在我的自定義導航控制器中重寫此方法,並且b)進入故事板,單擊自定義細節,並輸入CustomSegue作爲塞格級。不幸的是,由於我的目標是iOS 7,所以我收到了警告「僅在iOS 9之前自定義segues支持類名稱」,而自定義unwind segue現在不適用於iOS 7或iOS 8!

回答

3

經過大量的試驗和錯誤,我找到了一個解決方法。我注意到,當您覆蓋segueForUnwindingToViewController時,不再調用unwindForSegue:towardsViewController,這是問題所在。僅供參考,蘋果在UIViewController中的說明segueForUnwindingToViewController說:

已棄用。通過unwindForSegue:towardViewController:方法返回一個將從源展開到目標視圖控制器的繼續。當執行故事板中定義的unwind segue時,如果展開路徑上的任何視圖控制器已覆蓋此方法並返回非零,則運行時將使用該segue對象,而不是構造Interface Builder中指定的類的實例。

大膽的聲明的部分似乎並沒有得到執行,即如果重寫此方法,但返回nil,unwindForSegue:towardViewController仍然不叫和SEGUE不會發生。

爲了解決這個問題,我能夠在我的自定義導航控制器編輯segueForUnwindingToViewController

- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier 
{ 
    UIStoryboardSegue *segue; 
    if([fromViewController isKindOfClass:[MyViewController class]]){ 
     segue = [[CustomSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController]; //Custom Unwind Segue 
    } 
    else{ 
     if([[UIDevice currentDevice].systemVersion floatValue] < 9.0){ 
      return [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier]; //Normal Unwind Segue 
     } 
     else{ 
      [super unwindForSegue:segue towardsViewController:toViewController]; 
      return nil; 
     } 
    } 
    return segue; 
} 

UPDATE:調用[super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier];似乎仍是模態的展開塞格斯工作。我描述的問題似乎發生在演出中。因此,如果您的設備在版本< 9.0上運行,或者如果segue是模態的,則仍應調用舊方法。如果您在賽格模式下致電[super unwindForSegue:segue towardsViewController:toViewController];,賽格將不會工作/發生。

1

我稍微修改了你的代碼。
我不必考慮推或模態。
它似乎工作正常。

- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier 
{ 
    UIStoryboardSegue *segue; 
    if ([fromViewController isKindOfClass:[MyViewController class]]) { 
     segue = [[CustomSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController]; //Custom Unwind Segue 
    } 
    else { 
     if ([super respondsToSelector:@selector(unwindForSegue:towardsViewController:)]) { 
      [super unwindForSegue:segue towardsViewController:toViewController]; 
     } 
     segue = [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier]; 
    } 

    return segue; 
}