简体   繁体   English

iOS如何从框架启动ViewController

[英]iOS how to start a viewcontroller from framework

I have created my private framework successfully. 我已经成功创建了我的私有框架。 It has no error. 没有错误。 I have created a project now I want a simple thing but I do not have clue. 我现在已经创建了一个项目,我想要一个简单的东西,但是我没有头绪。 Following is what I want 以下是我想要的

I want to start the Framework view controller. 我想启动Framework视图控制器。 You can think it is the whole app. 您可以认为这是整个应用程序。 I want to start its first view controller. 我想启动它的第一个视图控制器。 and then the app flow is as per business logic. 然后按照业务逻辑进行应用程序流程。 I want to exit app when user exit the Framework main view controller. 我想在用户退出Framework主视图控制器时退出应用程序。 Actually it was the complete app but I decided to make it framework for different clients because only small modifications are needed for different clients but I do not know how it should be done. 实际上,它是完整的应用程序,但是我决定为不同的客户端构建框架,因为不同的客户端只需要进行少量修改,但是我不知道应该怎么做。

here is how I am trying to start first view controller in my Client project but it does not recognize that controller. 这是我尝试在Client项目中启动第一个视图控制器的方法,但是它无法识别该控制器。

 @IBAction func onClickBtnStartOver(_ sender: Any)
 {
     let storyBoard : UIStoryboard = UIStoryboard(name: "Storyboard", bundle:nil)            
     let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("idFrameworkVC") as FwViewController
     self.presentViewController(nextViewController, animated:true, completion:nil)
 }

It gives me error like " Use of undeclared type 'FwViewController' " 它给我类似“ 使用未声明的类型'FwViewController'的错误

So what should I do to start the first viewcontroller of framework. 所以我应该怎么做才能启动框架的第一个viewcontroller。

Note: I have viewcontrollers in one Storyboard namely Storyboard inside Framework 注意:我在一个Storyboard中有ViewController,即Framework内部的Storyboard。

尝试这个:

          UIApplication.shared.keyWindow?.rootViewController =  UIStoryboard(name: "yy", bundle: nil).instantiateInitialViewController()

Declare an open func in your framework for opening first viewController : 在您的框架中声明一个打开的func来打开第一个viewController:

open func presentFirstViewControllerOn(_ viewController:UIViewController) {
    let storyBoard : UIStoryboard = UIStoryboard(name: "Storyboard", bundle:nil)
    let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("idFrameworkVC") as FwViewController
    viewController.presentViewController(nextViewController, animated:true, completion:nil)
}

and call this method from client app's VC. 然后从客户端应用的VC调用此方法。 Deciding which is the first VC (if needed) should be the responsibility of the framework, not your client app. 确定哪个是第一个VC(如果需要)是框架的责任,而不是您的客户端应用程序的责任。

If everything is setup nicely then you may have not imported your framework in it. 如果一切设置都很好,那么您可能尚未将框架导入其中。 This is very important part. 这是非常重要的部分。 Make double check if You have declared Public your framework VC. 仔细检查是否已将Public声明为您的框架VC。 Then After it you need to start It in you method. 然后在它之后,您需要在您的方法中启动它。

I will suggest to see this selected answer. 我建议您查看选定的答案。 And this is the case with you 这就是你的情况

PS: Do not forget to go through into comments on the selected answer as there is discussion about crash. PS:不要忘记对所选答案进行评论,因为有关于崩溃的讨论。 I doubted that you face them, But if you did encounter any crash then fix the issue using that comment in answer 我怀疑您是否会遇到这些问题,但是,如果您遇到任何崩溃,请在回答中使用该注释来解决问题

use your framework bundle identifier 使用您的框架包标识符

 let frameworkBundle = Bundle(identifier: "your framework bundle identifier")
 let storyboard = UIStoryboard(name: "your framework storyboard name", bundle: frameworkBundle)
 let mas = storyboard.instantiateInitialViewController() as! UIViewController
 self.present(mas, animated: true, completion: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM