How to show my cocoa touch framework storyboard screen?

百般思念 提交于 2019-11-30 12:18:49

问题


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)

回答1:


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

Here's how. First some initial conditions:

  • Let's say your framework is called Coolness.

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

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

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

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)

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).




回答2:


I had the same problem. This is how I solved after a little research:

1 - I have a framework called "Messenger.framework"

2 - Inside it, I have a storyboard file called "Messenger.Storyboard"

3- My initial view controller is a UINavigationController with a rootViewController and my ChatController.

So this is how a present my framework's chat UIViewController:

- (void)presentChatController
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Messenger"
                                                         bundle:[NSBundle bundleForClass:ChatController.class]];

    ChatController *controller = [storyboard instantiateViewControllerWithIdentifier:@"Chat"];
    [self.navigationController pushViewController:controller animated:YES];
}

I hope this helps you.




回答3:


If your framework name is MyFramework, and storyboard name is Main, with a viewcontoller with storyboard id vc

if let urlString = NSBundle.mainBundle().pathForResource("MyFramework", ofType: "framework", inDirectory: "Frameworks") {
  let bundle = (NSBundle(URL: NSURL(fileURLWithPath: urlString)))
  let sb = UIStoryboard(name: "Main", bundle: bundle)
  let vc = sb.instantiateViewControllerWithIdentifier("vc")
  self.showViewController(vc, sender: nil)
}

I have inculded the framework using cocoapods.



来源:https://stackoverflow.com/questions/30287935/how-to-show-my-cocoa-touch-framework-storyboard-screen

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