xcode 如何从视图控制器加载故事板中的场景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13198227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load a scene in a storyboard from a view controller
提问by bitsdisasters
I am developing an application that loads a web page (using UIWebView) using Storyboard (I do know nothing about previous xib neither). I have already created a view controller for that UIWebView and everything works fine. The thing is: since previous versions of iOS don't allow to upload files, I need to make a new view (scene I thought it is called) that allows the user to pick and post a picture. I am able to develop both views separately and they work as expected but now I need to connect them based on event triggered when user wants to post a picture to the server. Using shouldStartLoadWithRequest I can catch that action, then I need to redirect to new view (which contains image picker and a button in order to upload the selected image) if iOS version is below 6.0 but I am really lost when it comes to load the new controller to show that view. Using buttons it is trivial but I don't know how to called inside the code. So far, I have a view controller linked to that scene and I have tried these ways:
我正在开发一个使用 Storyboard 加载网页(使用 UIWebView)的应用程序(我对以前的 xib 也一无所知)。我已经为该 UIWebView 创建了一个视图控制器,一切正常。问题是:由于以前版本的 iOS 不允许上传文件,我需要创建一个新视图(我认为它被称为场景),允许用户选择和发布图片。我能够分别开发两个视图并且它们按预期工作,但现在我需要根据用户想要将图片发布到服务器时触发的事件连接它们。使用 shouldStartLoadWithRequest 我可以捕获该操作,然后如果 iOS 版本低于 6,我需要重定向到新视图(其中包含图像选择器和一个按钮以上传所选图像)。0 但在加载新控制器以显示该视图时,我真的很迷茫。使用按钮很简单,但我不知道如何在代码中调用。到目前为止,我有一个链接到该场景的视图控制器,我尝试了以下方法:
WritePostViewController *postViewController = [[WritePostViewController alloc] init];
[self.navigationController pushViewController:postViewController animated:YES];
And even calling the storybard:
甚至调用storybard:
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
[sb instantiateInitialViewController];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"WritePostView"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
The first approach does nothing and second one shows this error log:
第一种方法什么都不做,第二种方法显示此错误日志:
*WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: Storyboard () doesn't contain a view controller with identifier 'WritePostView'*
*WebKit 丢弃了 webView:decidePolicyForNavigationAction:request:frame:decisionListener:delegate: Storyboard () 不包含带有标识符“WritePostView”的视图控制器中的未捕获异常*
I have been browsing and reading a lot but nothing solves my problem. For sure this a problem with my not-so-large knowledge about iOS but I am really stuck. I will thank any help.
我一直在浏览和阅读很多,但没有解决我的问题。这肯定是我对 iOS 不太了解的一个问题,但我真的被困住了。我会感谢任何帮助。
By the way, I need to come back after posting the file but I could imagine it is the same way opposite direction, right?
顺便说一句,我需要在发布文件后回来,但我可以想象它是相反的方向,对吧?
采纳答案by Phillip Mills
If your code is inside a view controller (which it seems to be), you can get the current storyboard with self.storyboard
. You also don't need instantiateInitialViewController
because, if all your UI is coming from the same storyboard, it has already gone through the loading of the initial controller.
如果您的代码在视图控制器内(看起来是这样),您可以使用self.storyboard
. 您也不需要,instantiateInitialViewController
因为如果您的所有 UI 都来自同一个故事板,那么它已经完成了初始控制器的加载。
As for the actual error, it's complaining that @"WritePostView" isn't a recognized name for any view controller in the storyboard. Note that what it looks for here is notthe class name for the controller but the Storyboard ID for the controller. (Which makes sense since you could have different "scenes" with the same typeof controllers.)
至于实际的错误,它抱怨 @"WritePostView" 不是故事板中任何视图控制器的公认名称。请注意,它在这里查找的不是控制器的类名,而是控制器的 Storyboard ID。(这是有道理的,因为您可以使用相同类型的控制器拥有不同的“场景” 。)
回答by bitsdisasters
Ok, it seems is solved. I just have added a seguebetween scenes
好的,好像解决了。我只是在场景之间添加了一个segue
and then I just need to add this one:
然后我只需要添加这个:
[self performSegueWithIdentifier:@"writePostViewSegue" sender:self];
and it works!. Anyway, I am not sure if it is the way to do it, so if someone knows better, please let me know.
它有效!无论如何,我不确定这是否是这样做的方式,所以如果有人知道更好,请告诉我。
Cheers
干杯