iOS頁面越級跳轉、指定頁面跳轉

iOS頁面跳轉方式

  1. [self presentViewController:viewController animated:YES completion:nil];
  2. [self.navigationController pushViewController:viewController animated:YES];

通常使用第2種方式,NavigationController已經幫我們管理了頁面跳轉形成的view controller的棧并且也提供了跳轉、回退到棧內指定頁面的方法:

//返回到上一個頁面
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;

//返回到根頁面
- (NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated;

//返回到棧內指定的UIViewController頁面
- (NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
//返回到棧內指定的UIViewController時,棧內該UIViewController上面的所有頁面都會出棧,并且參數viewController必須要保證在棧內

//跳轉到指定頁面常用方式1:
for (UIViewController *vc in self.navigationController.viewControllers) {
     if ([vc isKindOfClass:[yourTargetViewController class]]) {
           [self.navigationController popToViewController:vc animated:YES];
           return;
     }
 }

//跳轉到指定頁面常用方式2(如果知道指定頁面在棧內索引位置):
UIViewController *targetVC = self.navigationController.viewControllers[1];
//UIViewController *targetVC = [self.navigationController.viewControllers objectAtIndex:1];
[self.navigationController popToViewController:targetVC animated:YES];

使用第1種presentViewController方式需要先理解:

//UIViewController內有屬性
@property(nonatomic, readonly) UIViewController *presentingViewController;
@property(nonatomic, readonly) UIViewController *presentedViewController;

When you present a view controller modally (either explicitly or implicitly) using the presentViewController:animated:completion:method, the view controller that called the method has this property set to the view controller that it presented. If the current view controller did not present another view controller modally, the value in this property is nil.
簡單來說就是:
[controllerA presentViewController:controllerB animated:YES completion:nil];  
controllerB.presentingViewController = controllerA;
controllerA.presentedViewController = controllerB;
controllerB.presentedViewController = nil;

理解上面的屬性之后再看函數:

- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion;

官方說明:

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.
If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.
If you want to retain a reference to the view controller'??s presented view controller, get the value in the presentedViewControllerproperty before calling this method.
The completion handler is called after the viewDidDisappear:method is called on the presented view controller.

通俗點理解就是:
頁面controllerB的dismiss由展示它的頁面controllerB.presentingViewController負責:

//在controllerB內執行下面的方法其實是由controllerB.presentingViewController負責處理的
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
//上述兩行代碼的效果是一樣的,都是退出controllerB頁面

如果使用present方式展示了很多個頁面,形成了一個由presentedViewController形成的棧。
若在棧內某個viewControllerD(非棧頂)調用了該方法,將會dismiss它的直接child即(viewControllerD.presentedViewController)以及其上面的所有viewController。

綜合上面的信息就可以實現跳轉到指定viewContoller:

//直接回到根視圖
UIViewController *rootVC = self.presentingViewController;
while (rootVC.presentingViewController) {
   //根視圖的presentingViewController為nil退出循環
   rootVC = rootVC.presentingViewController;
}
[rootVC dismissViewControllerAnimated:YES completion:nil];

//跳轉到指定的targetViewController
UIViewController *targetVC = self.presentingViewController;
 while ( ![targetVC isKindOfClass:[targetViewController class]] ) {
    targetVC = targetVC.presentingViewController;
 }
[targetVC dismissViewControllerAnimated:YES completion:nil];
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,837評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,196評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 175,688評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,654評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,456評論 6 406
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,955評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,044評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,195評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,725評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,608評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,802評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,318評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,048評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,422評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,673評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,424評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,762評論 2 372

推薦閱讀更多精彩內容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,388評論 0 10
  • 文章的由來: 偶然在一次想要判斷當前控制器是通過present模態推出還是通過push入棧顯示的時候注意到了pre...
    東健FO_OF閱讀 4,890評論 0 14
  • 每個人都有所短,有所長。我的天賦是什么呢? 我從小熱愛運動,四肢修長,肌肉發達。前幾天還跟18歲在學校田徑隊的大兒...
    李筱玲閱讀 257評論 0 0
  • 我所說的門,不僅僅是手術室的那扇門也是生與死的那扇門。 2018.5.7 15:30 我們六個人照常在急診...
    鹿安和閱讀 610評論 3 3
  • “龍生龍,鳳生鳳,老鼠的兒子會打洞”這句俗語我初次聽到時,覺得很刺耳,但是,仔細一咀嚼,就又不得不贊同國人的智慧了...
    閑散25劃閱讀 123評論 0 0