Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

ios - UIStoryboard: What's the Correct Way to Get the Active Storyboard?

I am currently furiously digging through all the docs, and haven't quite found what I'm looking for. I suspect it is a real d'oh! answer.

I simply need to find the active storyboard in the main bundle, and want to know the best way to do this.

This is so that I can use the [UIStoryboard storyboardWithName:@"XXX" bundle:mainBundle] to extract the running storyboard.

I know how to kludge it by switching on the idiom, but I feel that this is a...kludge.

What's a correct way of doing this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In case you want to get the active storyboard for a viewController, there's a storyboard property. This is how I solved it, instead of making a new instance:

LoginViewController *vc = [navController.storyboard instantiateViewControllerWithIdentifier:@"firstLaunch"];
[navController presentModalViewController:vc animated:YES];

In Swift you'd call:

let loginViewController = navigationController?.storyboard?.instantiateViewController(withIdentifier: "firstLaunch") as! LoginViewController
navigationController?.present(loginViewController, animated: true, completion: nil)

You could also be a lot safer by using guards against the navigation controller and the storyboard. I've used as! so as to guarantee that you're getting a LoginController.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...