如何从视图控制器加载故事板中的场景 [英] How to load a scene in a storyboard from a view controller

查看:175
本文介绍了如何从视图控制器加载故事板中的场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,使用故事板加载网页(使用UIWebView)(我也不知道以前的xib)。我已经创建了一个视图控制器的UIWebView和一切工作正常。事情是:由于以前的iOS版本不允许上传文件,我需要做一个新的视图(场景,我认为它叫),允许用户选择和发布图片。我能够单独开发两个视图,并且它们按预期工作,但现在我需要基于事件触发时连接他们,当用户想要张贴图片到服务器。使用shouldStartLoadWithRequest我可以捕获该操作,然后我需要重定向到新的视图(其中包含图像选择器和一个按钮,为了上传所选的图像)如果iOS版本低于6.0,但我真的失去了当它来加载新的控制器来显示该视图。使用按钮是微不足道的,但我不知道如何调用代码内。到目前为止,我有一个视图控制器链接到那个场景,我已经尝试这些方法:

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:

WritePostViewController *postViewController = [[WritePostViewController alloc] init];
[self.navigationController pushViewController:postViewController animated:YES];

甚至打电话故事书:

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在webView中放弃了未捕获的异常:decisionPolicyForNavigationAction:request:frame:decisionListener:delegate:Storyboard()不包含标识为WritePostView的视图控制器*

* WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: Storyboard () doesn't contain a view controller with identifier 'WritePostView'*

我一直在浏览和阅读很多,但没有解决我的问题。肯定这是一个问题,我不是那么大的知识iOS,但我真的卡住了。我会感谢任何帮助。

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.

顺便说一下,我需要在发布文件后回来,但我可以想象它是相同的方向相反的方向,对吗? p>

By the way, I need to come back after posting the file but I could imagine it is the same way opposite direction, right?

推荐答案

如果你的代码是在一个视图控制器(它似乎是),你可以得到当前的故事板 self.storyboard 。你也不需要 instantiateInitialViewController ,因为如果你的所有UI来自同一个故事板,它已经经历了初始控制器的加载。

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.

对于实际的错误,它抱怨@WritePostView不是一个公认的名称为任何视图控制器的故事板。请注意,这里查找的是控制器的类名,而是控制器的故事板ID。 (这是有意义的,因为你可以有不同的场景与相同的类型的控制器。)

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 not the class name for the controller but the Storyboard ID for the controller. (Which makes sense since you could have different "scenes" with the same type of controllers.)

这篇关于如何从视图控制器加载故事板中的场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆