從VC1模態(tài)跳轉(zhuǎn)到VC2再從VC2跳轉(zhuǎn)到VC3后,使用方法dismissViewControllerAnimated只能從VC3跳轉(zhuǎn)到VC2,那么如何才能直接跳轉(zhuǎn)到VC1?
[self.presentingViewController.presentingViewController.presentingViewControllerdismissViewControllerAnimated:YEScompletion:nil];
使用上述方法可跳回到VC1,此方法在原理上也是遵循著VC3—VC2—VC1跳轉(zhuǎn)邏輯,只是在VC2的過程中給隱藏掉了。
在這里要注意的是presentingViewController 和presentedViewController的區(qū)別;在蘋果官方文檔中對其有如下注釋
// The view controller that was presented by this view controller or its nearest ancestor. ?這個(gè)視圖控制器是由他最近的父視圖控制器模態(tài)跳轉(zhuǎn)來
@property(nullable,nonatomic,readonly)UIViewController*presentedViewController
// The view controller that presented this view controller (or its farthest ancestor.)顯示此視圖控制器(或它的最上級父視圖控制器)的視圖控制器。
@property(nullable,nonatomic,readonly)UIViewController*presentingViewController
所以在此可以使用presenting跳轉(zhuǎn)回任意上級的視圖控制器,只需要調(diào)用多個(gè)presentingViewController方法即可實(shí)現(xiàn)。