Segue to another storyboard?

*爱你&永不变心* 提交于 2019-11-26 06:52:01

问题


Is it possible to segue from one storyboard to another, or to embed a storyboard in a view controller in another storyboard? I need to place a UITabBarController in a UINavigationController, and I\'d like to keep them nice and separate.


回答1:


Yes, but you have to do it programmatically:

// Get the storyboard named secondStoryBoard from the main bundle:
UIStoryboard *secondStoryBoard = [UIStoryboard storyboardWithName:@"secondStoryBoard" bundle:nil];

// Load the initial view controller from the storyboard.
// Set this by selecting 'Is Initial View Controller' on the appropriate view controller in the storyboard.
UIViewController *theInitialViewController = [secondStoryBoard instantiateInitialViewController];
//
// **OR**  
//
// Load the view controller with the identifier string myTabBar
// Change UIViewController to the appropriate class
UIViewController *theTabBar = (UIViewController *)[secondStoryBoard instantiateViewControllerWithIdentifier:@"myTabBar"];

// Then push the new view controller in the usual way:
[self.navigationController pushViewController:theTabBar animated:YES];



回答2:


From Xcode 7 onwards, you can do this graphically by using a Storyboard Reference:

Add Storyboard Reference to your storyboard. Create segue between ViewController and Storyboard Reference (ctrl + drag)

Then fill this fields.

Where "Tutorial" is "Tutorial.storyboard" file and "MainTutorialController" is your "Storyboard ID" field in ViewControllerSettings




回答3:


You can't really do segues manually because UIStoryboardSegue is an abstract class. You need to subclass it and implement perform in order for it to do anything. They're really meant to be created in storyboards. You can push the view controller manually, though, which is a good solution. lnafziger's answer does this well:

UIStoryboard *secondStoryBoard = [UIStoryboard storyboardWithName:@"secondStoryBoard" bundle:nil];
UIViewController *theTabBar = [secondStoryBoard instantiateViewControllerWithIdentifier:@"myTabBar"];
[self.navigationController pushViewController:theTabBar animated:YES];

One thing to note, though, is that you've said you want to keep things nice and separate. The idea of storyboards is to allow you to keep things separate while doing all of your design work in one place. Each view controller is nice and separated within the storyboard from the others. The whole idea is to keep it all in one place. Just lay it out nicely so that it's organized, and you'll be good to go. You shouldn't separate it unless you have a really good reason to do so.




回答4:


You should not place UITabBarControllers in a UINavigationController. It's asking for bugs such as incorrect autorotation/view unloading etc., as Apple doesn't support this sort of containment:

When combining view controllers, however, the order of containment is important; only certain arrangements are valid. The order of containment, from child to parent, is as follows:

  • Content view controllers, and container view controllers that have flexible bounds (such as the page view controller)
  • Navigation view controller
  • Tab bar controller
  • Split view controller



回答5:


Here is a swift version:

let targetStoryboardName = "Main"
let targetStoryboard = UIStoryboard(name: targetStoryboardName, bundle: nil)
if let targetViewController = targetStoryboard.instantiateInitialViewController() {
    self.navigationController?.pushViewController(targetViewController, animated: true)
}



回答6:


did you try the following :

2/ click to select your view controller which is linked to your navigation controller and in top menu : editor -> embed in -> Tab Bar controller

Note : I didn't test it because I'm using the opposite : making tabbed bar apps and puting navigation controller inside).



来源:https://stackoverflow.com/questions/9575702/segue-to-another-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!