2013-03-07 99 views
3

我們如何以編程方式執行xcode展開segue?xcode以編程方式展開segue

我知道簡單的方法是使用故事板並將按鈕拉到'退出',但我打算做的是在退出視圖之前先運行一些代碼。

+1

Po可以複製[如何以編程方式執行Unwind segue?](https://stackoverflow.com/questions/12509422/how-to-perform-unwind-segue-programmatically) – 2017-12-05 14:51:14

回答

5

要返回到RootViewController的使用:

[self.navigationController popToRootViewControllerAnimated:(BOOL)] 

或者去一個特定的控制器:

[self.navigationController popToViewController: 
    [[self.navigationController viewControllers] 
    objectAtIndex:THEINDEXOFTHEVIEWCONTROLLERTOUNWINDTO] 
    animated:YES]; 

對於更多的細節,請參見我在這篇文章中的回答: Push to root View controller in UIStoryboard

+0

感謝您的回答! – dan 2013-03-07 14:03:11

1

你可以有一個賽格瑞通過調用UIViewController中這種方法

- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender 

你仍然在2次之間的故事板創建SEGUE出現在代碼中,只要確保你的名字和賽格瑞使用在標識符參數

+0

最佳答案。最簡單的,而實際上使用unwind segues! – 2014-07-24 13:33:15

+1

但是,你是不是將推送的VC推入堆棧,而不是移除當前的VC,並讓呈現的VC的前一個實例進入堆棧的頂部? – 2014-12-11 17:39:03

4

如果你只是想退出之前執行一些代碼的確切名稱(雖然保持簡單,否則,直到返回的東西,你會得到一個UI鎖定),您可以覆蓋你的目標控制器上canPerformUnwindSegueAction:fromViewController:withSender:

像這樣的東西可能:

- (BOOL)canPerformUnwindSegueAction:(SEL)action 
       fromViewController:(UIViewController *)fromViewController 
         withSender:(id)sender 
{ 
    // Some operation... 

    return YES; // Or NO if something went wrong and you want to abort 
} 

否則,你實際上可以編程方式創建SEGUE並通過覆蓋segueForUnwindingToViewController:fromViewController:identifier:管理動畫/開卷邏輯。

您可以找到complete documentation here