finish iOS UIViewController like Android

相信Android開發者都用過 finish() 將Activity從stack中銷毀,另一方就沒那麼幸運了,Objectice-C可沒有finish()讓開發者一指搞定。

情境描述如下
  A1ViewController
前往A2ViewController
前往A3ViewController
前往B1ViewController
前往B2ViewController,並且銷毀B1ViewController

本來的順序A1→A2→A3→B1→B2
後來的順序A1→A2→A3B2

要達到目標有二種作法
●自訂delegate
    在B1ViewController自訂delegate
    在A3ViewControoler實作delegate

    B1ViewController觸發delegate時呼叫
    [self.navigationController popViewControllerAnimated:NO];

    A3ViewController接到delegate時打開B2ViewController
    [self.navigationController pushViewController:B2ViewController animated:YES];

●UINavigationController的setViewControllers方法
    NSMutableArray * viewControllers = [self.navigationController.viewControllers mutableCopy];
    [viewControllers removeLastObject];
    [viewControllers addObject:B2ViewController];
   
    [self.navigationController setViewControllers:viewControllers animated:YES];


參考資料
equivalent of startactivity & finish in ios?
UINavigationController的setViewControllers方法

沒有留言: