如何展示我的可可触摸框架故事板屏幕? [英] How to show my cocoa touch framework storyboard screen?

查看:107
本文介绍了如何展示我的可可触摸框架故事板屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用故事板创建了一个简单的Cocoa触摸框架。在我的框架中,我有一个MainViewController.swift viewcontroller。
我创建了一个新的单一视图项目,导入了我的框架并尝试加载我的框架viewcontroller,但我得到了黑屏。而且我不知道为什么。

I created a simple Cocoa touch framework with a storyboard. In my framework i have a MainViewController.swift viewcontroller. I created a new single view project, imported my framework and tried to load my framework viewcontroller, but i got black screen. And I dont know why.

我尝试使用以下代码加载框架:

I tried load framework with this code:

let frameworkScreen : UIViewController = MainViewController()
    self.presentViewController(frameworkScreen, animated: true, completion: nil)


推荐答案

您需要通过从框架中的故事板中实例化来加载视图控制器。

You need to load the view controller by instantiating it from the storyboard in the framework.

这是怎么回事。首先是一些初始条件:

Here's how. First some initial conditions:


  • 假设您的框架名为Coolness。

  • Let's say your framework is called Coolness.

假设你的框架的故事板叫做 CoolnessStoryboard.storyboard

Let's say your framework's storyboard is called CoolnessStoryboard.storyboard.

假设你的框架有一个 public 类叫做CoolnessViewController。

Let's say your framework has a public class called CoolnessViewController.

让我们说CoolnessStoryboard有一个场景,其视图控制器是CoolnessViewController,这是它的初始视图控制器。

Let's say that CoolnessStoryboard has a scene whose view controller is CoolnessViewController and that this is its initial view controller.

然后在主代码中你会 import Coolness 并且,为了从故事板中展示你的CoolnessViewController,你会说:

Then in your main code you would import Coolness and, to present your CoolnessViewController from the storyboard, you would say:

let s = UIStoryboard (
    name: "CoolnessStoryboard", bundle: NSBundle(forClass: CoolnessViewController.self)
)
let vc = s.instantiateInitialViewController() as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)

注意策略在这里。从目标向后工作。我们需要在故事板中的CoolnessViewController实例。为此,我们需要对该故事板的引用。为此,我们需要确定故事板。怎么样?我们可以通过名称和它所在的包来识别它。但是我们如何识别该包呢?我们可以通过该捆绑中的类来识别捆绑包。我们这样的类,因为我们已经导入了框架( import Coolness ),那里的类是 public (所以我们可以说)。

Note the strategy here. Work backwards from the goal. We need the instance of CoolnessViewController that is in the storyboard. To get that, we need a reference to that storyboard. To do that, we need to identify that storyboard. How? We can identify it by name and by the bundle that it is in. But how can we identify that bundle? We can identify the bundle by means of a class in that bundle. We have such a class, because we have imported the framework (import Coolness) and the class there is public (so we can speak of it).

这篇关于如何展示我的可可触摸框架故事板屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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