![](/img/trans.png)
[英]How to call a method in the rootviewcontroller from a presented modal view controller at the time of dismissing the model view
[英]Back to RootViewController from Modal View Controller
從Home視圖 - 我的RootViewController - 當用戶在導航層次結構中進行時,我一個接一個地打開2個ViewControllers,如下所示:
1)通過我的Storyboard中連接的按鈕推送SecondViewController
2)ThirdViewController以模態呈現
[self performSegueWithIdentifier:@"NextViewController" sender:nil];
所以,圖片是:RootViewController - > SecondViewController - > ThirdViewController
現在在我的ThirdViewController中,我希望有一個按鈕可以返回2次到我的RootViewController,即回家。 但這不起作用:
[self.navigationController popToRootViewControllerAnimated:YES];
只有這個人回到SecondViewController一次
[self.navigationController popViewControllerAnimated:YES];
如何同時刪除模態和推送視圖控制器?
我有類似的情況,我將許多視圖控制器推入導航控制器堆棧,然后最后一個視圖以模態方式呈現。 在模態屏幕上,我有一個取消按鈕,返回到根視圖控制器。
在模態視圖控制器中,我有一個在點擊取消按鈕時觸發的動作:
- (IBAction)cancel:(id)sender
{
[self.delegate modalViewControllerDidCancel];
}
在這個模態視圖控制器的標題中,我聲明了一個協議:
@protocol ModalViewControllerDelegate
- (void)modalViewControllerDidCancel;
@end
然后導航堆棧中的最后一個視圖控制器(呈現模態視圖的ModalViewControllerDelegate
)應該實現ModalViewControllerDelegate
協議:
- (void)modalViewControllerDidCancel
{
[self dismissViewControllerAnimated:NO completion:nil];
[self.navigationController popToRootViewControllerAnimated:YES];
}
上述方法是重要的部分。 它使呈現視圖控制器關閉模態視圖,然后彈出回根視圖控制器。 請注意,我將NO
傳遞給dismissViewControllerAnimated:並且將popToRootViewControllerAnimated傳遞給YES
:從模態視圖到根視圖獲得更平滑的動畫。
我有相同的要求,但在視圖控制器之間使用自定義segue。 我想到了“Unwind Segue”的概念,我認為它與iOS6一起出現。 如果您的目標是iOS6及更高版本,這些鏈接可能有所幫助: 什么是Unwind segues以及如何使用它們? http://chrisrisner.com/Unwinding-with-iOS-and-Storyboards謝謝。
假設您的AppDelegate被稱為AppDelegate,那么您可以執行以下操作,將視圖RootViewController重置應用程序窗口的rootviewcontroller
AppDelegate *appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate];
RootViewController *rootView = [[RootViewController alloc] init];
[appDel.window setRootViewController:rootView];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.