[英]Programatically return to previous ViewController in different storyboard
我在這里嘗試了很多答案,但沒有一個具有相同的確切場景。
我正在嘗試導航到一個UIViewController
,它位於另一個包中的獨立故事板中,到目前為止我能夠導航到它但是無法返回到之前的UIViewController
。 調用外部視圖控制器( TabBarController
)的方法實現如下:
+(void) launchExternalUI: (UIViewController *) previousViewController {
UIStoryboard* sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: [self frameworkBundle]];
TabBarController *vc = (TabBarController*) [sb instantiateInitialViewController];
[previousViewController.navigationController pushViewController:vc animated:YES];
[vc release];
}
現在TabBarController
中的方法應該返回到前一個視圖控制器:
- (void) navigateToPreviousViewController: (UIGestureRecognizer *)gesture {
[self.navigationController popViewControllerAnimated:YES];
}
在TabBarController
,如果我在self.navigationController
打印所有的viewcontrollers,我看到的只是TabBarController
,我不應該看到在launchExternalUI
上推送它的前一個視圖控制器嗎? [self.navigationController popViewControllerAnimated:YES];
完全沒有效果。 我對此有點失落。
同樣重要的是要注意previousViewController
是在本地故事板中定義的,而TabBarController
是在不同的.framework
,會導致問題嗎?
在此先感謝您的幫助!
**編輯:我需要的導航流程是storyboard1:VC1->storyboard2:VC2->storyboard1:VC1
,我可以獲得storyboard1:VC1->storyboard2:VC2
部分工作但不是storyboard2:VC2->storyboard1:VC1
我經常將項目拆分為各種故事板,並創建了一個動態視圖控制器,用於處理從輔助故事板加載適當控制器的任務,同時維護導航樹。
我已經創建了一個示例項目並上傳到github,因為它比解釋這里的所有步驟更容易。 需要注意的關鍵部分是Main.stoyboard中每個DynamicStoryboardViewControllers的用戶定義的運行時屬性。 另請注意,每個輔助故事板都需要為其中一個viewControllers選中“是初始視圖控制器”。 示例中不包括從故事板加載特定場景。 這只不過是添加“sceneName”動態運行時屬性,就像添加storyboardName屬性一樣。
這是一個快速的樣本,所以有點粗糙,但你會明白它是如何工作的。 如果你遇到問題,請隨時提問。
干杯!
編輯:
我突然意識到,您可能在視圖層次結構中沒有navigationController(就像我在我的示例中所做的那樣)。 無論如何,您似乎無法控制標簽欄的引入位置。 因此,如果沒有navigationController,[self.navigationController popViewControllerAnimated:YES]將無效;
你應該測試一下,然后像你一樣調用popViewControllerAnimated,或者調用dismissViewControllerAnimated;
if(self.navigationController){
[self.navigationController popViewControllerAnimated:YES];
}else{
[self.presentingViewController dismissViewControllerAnimated:Yes completion:nil];
}
希望這有幫助,如果沒有,也許你可以提供一些示例代碼。
在原始視圖控制器中創建展開segue,並使用segues而不是將視圖控制器視圖推送/彈出到視圖數組上。
要創建展開segue,您應該在ORIGINAL viewcontroller中創建一個展開方法:
- (IBAction)unwindToOriginalViewControllerSegue:(UIStoryboardSegue*)sender {
; //TODO: anything special you need, reference via sender
}
然后,在您的新視圖控制器中,在故事板中,從控制器圖標拖動到“退出”圖標。 鏈接到Unwind segue你在上面命名。
UIViewController * YourViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Storyboard Identifier"];
[self.navigationController pushViewController:YourViewController animated:TRUE];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.